AspNet Core 2.0 using Database First, Update Model

AspNet Core 2.0 using Database First, Update Model

As of Aspnet core there no longer any edmx to connect database first.

One article that I have tried and found to work using database first is this link

Just to summarize what was done in the link above with some corrections as of 25 Dec 2017:-

  1. After you create a Aspnet core 2.0 web application – mvc using razor
  2. Need to add the 3 following package using Nuget Tool commandline:
    • Install-Package Microsoft.EntityFrameworkCore.SqlServer
    • Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
    • Install-Package EntityFrameWorkCore.Tools
    • EntityFrmaeworkCore.SqlServer.Design (In link above it mention to add this package but there no need as this package is added automaticately when add 3 package above)
  3. After you add package in the commandline run the scaldfold command to create the database object tables
    • Scaffold-DbContext “Server=SSAI-L0028-HP\SQLEXPRESS;Database=EFCoreDBFirstDemo;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/DB (Change the place for the Server, Database and Output
  4. Next step is to register DBcontext using dependency injection
    • Open the *Context.cs
    • Remove the OnConfiguring() method
    • Add — Context if database name was Safe below just replact Context with SafeContext
      • public Context(DbContextOptions<Context> options)
      • : base(options)
      • { }
  5. Add theconnection string in appsetting.json
    • “ConnectionStrings”: {
    • i.  “EFCoreDBFirstDemoDatabase”“Server=SSAI-L0028-HP\\SQLEXPRESS;Database=EFCoreDBFirstDemo;Trusted_Connection=True;”
    • }   —–> (this written in the link above but it slightly wrong it)
    • “ConnectionStrings”: {
    • “SafeDatabase”“Server=SSAI-L0028-HP\\SQLEXPRESS;Database=EFCoreDBFirstDemo;Trusted_Connection=True;”
    • }     —> remember to update Connections string Name “SafeDatabase” Server, Database
  6. In Startup.cs – add your namespace for models, DB, and EntityFrameworkcore
    • using WebApplicationCoreMvc.Models;
    • using WebApplicationCoreMvc.Models.DB;
    • using Microsoft.EntityFrameworkCore;
  7. In Startup.cs – add the following in  ConfigureServices() method
    • services.AddDbContext<Context>(options => options.UseSqlServer(Configuration.GetConnectionString(“SafeDatabase”)));

In the link above it also show the scaffolding Entity in the Project. It seem to work as documents so I won’t add any notes here.

Updating Database Model.

Something that was not added in the link above was what if you make a changes in the database schema, say update a field or add a new table. How do update the model in AspNet Core.

Ans: Just rerun the command in nuget console that was outlined in step 5 but include a -Force.

Scaffold-DbContext “Server=SSAI-L0028-HP\SQLEXPRESS;Database=EFCoreDBFirstDemo;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/DB -Force

 

Leave a Reply

Your email address will not be published. Required fields are marked *