How to Install Entity Framework and Configure in Asp.Net

First part of Entity Framework Link

In this article i will explain How to Install Entity Framework and Configure for sql server in asp.net core mvc

How to install Entity Framework Core in your web application?

In asp.net core we have meta-package and it does not have all dll it’s own it is only list of dependencies. Means it’s a list of dependencies. From 2.1 version of asp.net core we have no need to add an entity framework package. Our meta-package already contains this as part of the meta-package.

List Of Entity Framework Packages:-

  1. Microsoft.EntityFrameworkCore.SqlServer– In this package we have specific functionality in respect with SQL server.
  2. Above package have dependency on this package Microsoft.EntityFrameworkCore.Relational which contain relation database-related functionality.
  3. This package has dependencies on Microsoft.EntityFrameworkCore So this package core functionality which is common to all databases.

DbContext

It is one of the most important class in Entity Framework. It is used to make connection and retrieve data from database. To create a class we can of DbContext we write a class and it inherit from DbContext class which is present in Entity Framework.

public class OurDbContext:DbContext
    {
    }

And we need to import below dll Microsoft.EntityFrameworkCore for DbContext.

DbSet()

The DbSet class is used to represent entity set which can be used for create, update, read and delete operations. Now the context which is inherited from DbContext class needs to include DbSet type property for mapping the database tables, views to entity.

How many tables you want to used you need to create separate DbSet property for each table.

public class OurDbContext:DbContext
    {
        public DbSet<Student> Students { get; set; }
        public OurDbContext(DbContextOptions<OurDbContext> options):base(options)
        {
                
        }
    }

In this above example suppose we fire LINQ queries against the DbSet<Student> will be translated into queries against the underlying database.

When we want to pass configuration information to our DbContext we use DbContextOptions object.

How to configure Entity Framework Core for SQLServer ?

Entity framework core support variety of databases which includes relational as well as non-relational.

To configure DbContext we have to use ConfigureService method in Startup class.

Configure Entity Framework for SQL SERVER

To configure this we have 2 methods as shown above AddDbContextPool and AddDbContext.

services.AddDbContextPool<OurDbContext>(options => options.UseSqlServer(""));

This line needs to add in ConfigureService method.we provide options as parameter which is used for giving connectionstring here but directly hard code here we put in a configuration file which is appsettings.json in our case and then we read it.

In classic asp.net we store connection string in the config file but in asp.net core we store it on the apsetting.json file.

Now to read from appsetting.json below code.

services.AddDbContextPool<OurDbContext>(options => options.UseSqlServer(_config.GetConnectionString("StudentDBString")));

In this first step is to register our class using asp.net core dependency injection system using AddDbCOntextPool method.

Then in the next step, we also provide UseSqlServer as a database provider for our application.

After this, we store the connection string in appsetting file and read it by using IConfiguration object which is injected In the constructor of that startup class.

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts