How to Use EF Core with ASP.NET Core

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

  1. Open Visual Studio Code or Visual Studio 2019 and create a new ASP.NET Core Web Application project.
  2. Choose the "Web Application" template and click "Create".
  3. Select "ASP.NET Core 5.0" as the target framework and choose "API" as the project template.
  4. Click "Create" to create the project.

Adding EF Core to the Project

  1. 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
  2. Create a new folder named "Models" in your project.
  3. Create a new class in the "Models" folder and name it "MyContext". This will be our database context class.
  4. Inherit from the DbContext class in the Microsoft.EntityFrameworkCore namespace.
    using Microsoft.EntityFrameworkCore;
    
    public class MyContext : DbContext
    {
        // DbContext code here...
    }
  5. Add a constructor that takes a DbContextOptions parameter and passes it to the base constructor.
    public MyContext(DbContextOptions<MyContext> options)
        : base(options)
    {
    }
  6. 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.
  7. 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.
  8. 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

  1. Add a new controller to your project.

  2. Inject an instance of MyContext into the constructor of the controller.
    private readonly MyContext _context;
    
    public ProductsController(MyContext context)
    {
        _context = context;
    }
  3. 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.
  4. 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

Related posts

Add comment

Loading