Skip to content

tonysneed/Demo.AspNetCore2.EF6

Repository files navigation

Using Entity Framework 6 with ASP.NET Core 2.0

  1. Create a solution with Visual Studio 2017 and add a new .NET Class Library project.

    • Select Windows Classic Desktop, Class Library

    add-class-lib

    • Specify a .NET Framework version (for ex, 4.7)
    • Add the EF6 NuGet package.

    add-ef6-package

  2. Add a Product entity class to a Models folder.

    public class Product
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public decimal UnitPrice { get; set; }
    }
  3. Add a SampleDbContext to a Contexts folder and inherit from DbContext.

    • Add a Products property of type DbSet<Product>

      public class SampleDbContext : DbContext
      {
          public SampleDbContext(string connectionName) :
              base(connectionName) { }
      
          public DbSet<Product> Products { get; set; }
      }
  4. Add a SampleDbContextFactory class to the Contexts folder.

    • Implement IDbContextFactory

    • Return a new SampleDbContext in the Create method

    • Specify a connection string for the SampleDb database

      class SampleDbContextFactory : IDbContextFactory<SampleDbContext>
      {
          public SampleDbContext Create()
          {
              return new SampleDbContext(@"Server=(localdb)\mssqllocaldb;Database=SampleDb;Trusted_Connection=True;MultipleActiveResultSets=true");
          }
      }
  5. In Package Manager Console select the class library project

    • Run the following command: Enable-Migrations

    • Add code to the Seed method in the Configuration class in the Migrations folder

      context.Products.AddOrUpdate(
          new Product { Id = 1, ProductName = "Chai", UnitPrice = 10 },
          new Product { Id = 2, ProductName = "Chang", UnitPrice = 11 },
          new Product { Id = 3, ProductName = "Aniseed Syrup", UnitPrice = 12 }
      );
    • Run the Add-Migration Initial command

    • Run the Update-Database command

    • Add a data connection to the Server Explorer

    • Verify that the SampleDb database exists and that the Products table contains data

  6. Add a new Web project to the solution.

    • Select .NET Core, ASP.NET Core Web Application

    add-new-web-project

    • Select Web API targeting the full .NET Framework

    new-aspnet-core

    • Set the web project as the startup project for the solution
    • Add the EF6 Nuget package to the web project
    • Add a project reference to the Data class library project

    add-project-ref

    • Build the solution.
  7. Open appsettings.json in the Web project and add the connection string

    "ConnectionStrings": {
        "SampleDb": "Server=(localdb)\\mssqllocaldb;Database=SampleDb;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  8. In the Web project register the context for DI in Startup.cs.

    • Add following code after services.AddMvc() in the ConfigureServices method

      services.AddScoped(provider =>
      {
          var connectionString = Configuration.GetConnectionString("SampleDb");
          return new SampleDbContext(connectionString);
      });
  9. Add a ProductsController that extends Controller

    add-products-controller

    • Pass SampleDbContext to the ctor

    • Add actions for GET, POST, PUT and DELETE

      [Route("api/[controller]")]
      public class ProductsController : Controller
      {
          private readonly SampleDbContext _dbContext;
      
          public ProductsController(SampleDbContext dbContext)
          {
              _dbContext = dbContext;
          }
      
          // GET: api/products
          [HttpGet]
          public async Task<ActionResult> Get()
          {
              var products = await _dbContext.Products
                  .OrderBy(e => e.ProductName)
                  .ToListAsync();
              return Ok(products);
          }
      
          // GET api/products/5
          [HttpGet("{id}")]
          public async Task<ActionResult> Get(int id)
          {
              var product = await _dbContext.Products
                  .SingleOrDefaultAsync(e => e.Id == id);
              if (product == null)
                  return NotFound();
              return Ok(product);
          }
      
          // POST api/products
          [HttpPost]
          public async Task<ActionResult> Post([FromBody]Product product)
          {
              _dbContext.Entry(product).State = EntityState.Added;
      
              await _dbContext.SaveChangesAsync();
              return CreatedAtAction("Get", new { id = product.Id }, product);
          }
      
          // PUT api/products
          [HttpPut]
          public async Task<ActionResult> Put([FromBody]Product product)
          {
              _dbContext.Entry(product).State = EntityState.Modified;
      
              await _dbContext.SaveChangesAsync();
              return Ok(product);
          }
      
          // DELETE api/products/5
          [HttpDelete("{id}")]
          public async Task<ActionResult> Delete(int id)
          {
              var product = await _dbContext.Products
                  .SingleOrDefaultAsync(e => e.Id == id);
              if (product == null) return Ok();
      
              _dbContext.Entry(product).State = EntityState.Deleted;
              await _dbContext.SaveChangesAsync();
              return Ok();
          }
      }
  10. Test the controller by running the app and submitting some requests.

    • Use Postman or Fiddler

    • Set Content-Type header to application/json for POST and PUT.

    • The database should be created automatically

      GET: http://localhost:50781/api/products
      POST: http://localhost:50781/api/products
        - Body: {"productName":"Ikura","unitPrice":12}
      GET: http://localhost:50781/api/products/4
      PUT: http://localhost:50781/api/products/4
        - Body: {"id":4,"productName":"Ikura","unitPrice":13}
      DELETE: http://localhost:50781/api/products/4
      

About

Using Entity Framework 6 with ASP.NET Core 2.0

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages