.NET Core Code First Migration Using Class Library

Steps

  1. Create a new project.
  2. Create Class Library.
  3. Install Entity Framework.
  4. Create Model.
  5. Register context with dependency injection.
  6. Create database.

Let’s get started.

Create a new project with Visual Studio 2015.
  1. File > New > Project.
  2. In the left menu > Visual C# > Web.
  3. Choose ASP.NET Core Web Application (.NET Core).

    ASP.NET Core Web Application

Name it (MVCAngular2) and click OK. In our next step, we will see how to create a .NET Core Class Library & reference it to our application.

Add Class Library: SRC > Add > New Project.

New Project

From the left menu > Visual C# > .NET Core.

 .Net Core

Choose Class Library (.NET Core), name it as MVCAngular2.Models, and click OK.

Project.json default template

  1. {  
  2.     "version""1.0.0-*",  
  3.     "dependencies": {  
  4.         "NETStandard.Library""1.6.0"  
  5.     },  
  6.     "frameworks": {  
  7.         "netstandard1.6": {  
  8.             "imports""dnxcore50"  
  9.         }  
  10.     }  
  11. }  
We need to add command line tools for EF Core that include commands for Package Manager console.

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"

code

There is an unsupported issue of EF Core 1.0.0-preview2-final with "NETStandard.Library": "1.6.0", so that we have changed the target framework to netstandard1.6 > netcoreapp1.0.

We have also specified the command line tool in tools section.
  1. "tools": {  
  2.     "Microsoft.EntityFrameworkCore.Tools""1.0.0-preview2-final"  
  3. },  
Finally, after modification.
  1. {  
  2.     "version""1.0.0-*",  
  3.     "dependencies": {  
  4.         "Microsoft.EntityFrameworkCore.SqlServer""1.0.0",  
  5.         "Microsoft.EntityFrameworkCore.Tools""1.0.0-preview2-final"  
  6.     },  
  7.     "tools": {  
  8.         "Microsoft.EntityFrameworkCore.Tools""1.0.0-preview2-final"  
  9.     },  
  10.     "frameworks": {  
  11.         "netcoreapp1.0": {  
  12.             "imports": ["dotnet5.6""portable-net45+win8"]  
  13.         }  
  14.     }  
  15. }  
Packages will be automatically restored to our project Class Library.

library

Install Entity Framework

Now, let’s add the folder for context and entities.

folder

Configuring DbContext

Let’s add a class to our context folder and name it StudentContext which is inherited from the base class DbContext. We need to reference Entity Framework Core library to our class.
  1. using Microsoft.EntityFrameworkCore;  
Here, in our context class, we have passed a generic version of DbContextOptions<T> with constructor argument of our context class.
  1. public class StudentContext: DbContext   
  2. {  
  3.     public StudentContext(DbContextOptions < StudentContext > options): base(options) {}  
  4. }  
Using base keyword, we have accessed the base class (DbContext) constructor to pass the options for the context from
DbContextOptions<T>.

base(options)


Let’s add Student Model in Entities folder.
  1. public class Student   
  2. {  
  3.     public int StudentID {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public string StudentName {  
  8.         get;  
  9.         set;  
  10.     }  
  11. }  
Now, in our DbContext Class, let’s define the dbSets with Student Model.

Finally DbContext class
  1. public class StudentContext: DbContext   
  2. {  
  3.     public StudentContext(DbContextOptions < StudentContext > options): base(options) {}  
  4.     public DbSet < Student > Students {  
  5.         get;  
  6.         set;  
  7.     }  
  8. }  
Register context with DI

Before registering the context, let’s reference the library to our Web application project.
  • Reference > Add Reference > Solution > MVCAngular2.Models

    Reference

Open Startup.cs file. In ConfigureServices method, we need to add AddDbContext method which uses Microsoft.Extensions.DependencyInjection to register StudentContext as a service.

  1. public void ConfigureServices(IServiceCollection services)   
  2. {  
  3.     var connection = @ "Server=DESKTOP-4T79RA1;Database=EFCoreStudentDb;Trusted_Connection=True;";  
  4.     services.AddDbContext < StudentContext > (options => options.UseSqlServer(connection));  
  5. }  
First, we have specified the connection info.
  1. var connection = @"Server=DESKTOP-4T79RA1;Database=EFCoreStudentDb;Trusted_Connection=True;";   
After that, we have registered the context using the connection info.
  1. services.AddDbContext<StudentContext>(options => options.UseSqlServer(connection));  
Here, UseSqlServer() is an extension method that configures the StudentContext to connect our SQL Server database.

Create database

Now, let’s create a database by using migrations.
  1. Tools > NuGet Package Manager > Package Manager Console.
  2. Type Add-Migration Initial to scaffold a migration, press Enter
  3. Type Update-Database to apply the new migration to the database.

Add-Migration Initial

Add-Migration Initial

Add-Migration Initial
Update-Database

Update-Database

Now, open SSMS in order to explore our newly created database. As you can see from the below image.

SSMS

Hope this will help.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now