Here is an example of creating a model, DbContext class, adding a connection string, adding services to Startup class, and running migrations in ASP.NET Core.
Create a Model:
Let’s say we want to create a model for a product entity, we can create a class called Product
in our project with the following properties:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
}
Create a DbContext class:
We’ll create a class called AppDbContext
that inherits from DbContext
. We’ll add a constructor that takes in a DbContextOptions<AppDbContext>
parameter and calls the base constructor with that parameter. We’ll also add a DbSet<Product>
property to represent our products table.
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
}
Add a Connection String:
We need to add a connection string to our appsettings.json
file that points to our database. Here’s an example of what the connection string might look like if we’re using a SQL Server database:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
...
}
Add Services to Startup Class:
In our Startup.cs
file, we’ll add the following code to the ConfigureServices
method:
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
This code tells ASP.NET Core to use SQL Server as our database provider and to use the connection string we defined in appsettings.json
. We’re also registering AppDbContext
as a service so that we can use it later in our controllers or other services.
Run Migrations:
To create our products table in the database, we need to run migrations. We can do this by opening the Package Manager Console in Visual Studio and running the following commands:
Add-Migration InitialCreate
Update-Database
The Add-Migration
command creates a new migration file based on the changes to our DbContext. The Update-Database
command applies the migration to the database, creating the products table.
That’s it! Now we have a working database with a products table that we can use in our ASP.NET Core application.
Here are some packages that are important for implementing Entity Framework in ASP.NET Core:
- Microsoft.EntityFrameworkCore: This is the core Entity Framework package that provides the main functionality for working with databases. It includes classes for defining models, DbContext, and LINQ-to-Entities queries.
- Microsoft.EntityFrameworkCore.Tools: This package provides tools for working with Entity Framework migrations. It includes the Add-Migration and Update-Database commands that we use to create and apply migrations.
- Microsoft.EntityFrameworkCore.SqlServer: This package provides support for using SQL Server as the database provider in Entity Framework.
- Microsoft.EntityFrameworkCore.Design: This package provides design-time support for Entity Framework. It includes the EF Core tools that we use in the Package Manager Console to create and apply migrations.
- Microsoft.Extensions.DependencyInjection: This package provides the dependency injection framework used by ASP.NET Core. We use this package to register our DbContext as a service in the Startup class.
These packages are all available on NuGet and can be installed using the Package Manager Console or the Visual Studio Package Manager.