Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. It is an object-relational mapper (ORM) that enables .NET developers to work with databases using .NET objects. In this article, we will explore how to use EF Core with ASP.NET Core.
Prerequisites
Before we get started, make sure you have the following installed on your machine:
- .NET Core SDK
- Visual Studio Code or Visual Studio 2019
Setting up the Project
- Open Visual Studio Code or Visual Studio 2019 and create a new ASP.NET Core Web Application project.
- Choose the "Web Application" template and click "Create".
- Select "ASP.NET Core 5.0" as the target framework and choose "API" as the project template.
- Click "Create" to create the project.
Adding EF Core to the Project
- Install the
Microsoft.EntityFrameworkCore.SqlServer
package using the NuGet Package Manager or by running the following command in the Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
- Create a new folder named "Models" in your project.
- Create a new class in the "Models" folder and name it "MyContext". This will be our database context class.
- Inherit from the
DbContext
class in the Microsoft.EntityFrameworkCore
namespace.
using Microsoft.EntityFrameworkCore;
public class MyContext : DbContext
{
// DbContext code here...
}
- Add a constructor that takes a
DbContextOptions
parameter and passes it to the base constructor.
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
- Add a
DbSet
property for each entity you want to map to the database.
public DbSet<Product> Products { get; set; }
Replace "Product" with the name of your entity.
- In the
Startup.cs
file, add the following code to the ConfigureServices
method to register the database context with the dependency injection container:
services.AddDbContext<MyContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyContext")));
This will enable us to use dependency injection to inject an instance of MyContext
wherever we need it in our application.
- In the
appsettings.json
file, add a connection string for your database.
"ConnectionStrings": {
"MyContext": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Replace "MyDatabase" with the name of your database.
Using EF Core in the Application
-
Add a new controller to your project.
- Inject an instance of
MyContext
into the constructor of the controller.
private readonly MyContext _context;
public ProductsController(MyContext context)
{
_context = context;
}
- Use LINQ queries to interact with the database.
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
{
return await _context.Products.ToListAsync();
}
Replace "Product" with the name of your entity.
- Run the application and navigate to the endpoint for the controller you created to see the results.
Conclusion
In this article, we explored how to use EF Core with ASP.NET Core. We added EF Core to the project