Steps
- Create a new project.
- Create Class Library.
- Install Entity Framework.
- Create Model.
- Register context with dependency injection.
- Create database.
Let’s get started.
Create a new project with Visual Studio 2015.
- File > New > Project.
- In the left menu > Visual C# > Web.
- Choose ASP.NET Core Web Application (.NET Core).
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.
From the left menu > Visual C# > .NET Core.
Choose Class Library (.NET Core), name it as MVCAngular2.Models, and click OK.
Project.json default template
- {
- "version": "1.0.0-*",
- "dependencies": {
- "NETStandard.Library": "1.6.0"
- },
- "frameworks": {
- "netstandard1.6": {
- "imports": "dnxcore50"
- }
- }
- }
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" 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.
- "tools": {
- "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
- },
Finally, after modification.
- {
- "version": "1.0.0-*",
- "dependencies": {
- "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
- "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
- },
- "tools": {
- "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
- },
- "frameworks": {
- "netcoreapp1.0": {
- "imports": ["dotnet5.6", "portable-net45+win8"]
- }
- }
- }
Packages will be automatically restored to our project Class Library.
Install Entity Framework Now, let’s add the folder for context and entities.
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.
- using Microsoft.EntityFrameworkCore;
Here, in our context class, we have passed a generic version of DbContextOptions<T> with constructor argument of our context class.
- public class StudentContext: DbContext
- {
- public StudentContext(DbContextOptions < StudentContext > options): base(options) {}
- }
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.
- public class Student
- {
- public int StudentID {
- get;
- set;
- }
- public string StudentName {
- get;
- set;
- }
- }
Now, in our DbContext Class, let’s define the dbSets with Student Model.
Finally DbContext class - public class StudentContext: DbContext
- {
- public StudentContext(DbContextOptions < StudentContext > options): base(options) {}
- public DbSet < Student > Students {
- get;
- set;
- }
- }
Register context with DI
Before registering the context, let’s reference the library to our Web application project.
- Reference > Add Reference > Solution > MVCAngular2.Models
Open Startup.cs file. In ConfigureServices method, we need to add AddDbContext method which uses Microsoft.Extensions.DependencyInjection to register StudentContext as a service.
- public void ConfigureServices(IServiceCollection services)
- {
- var connection = @ "Server=DESKTOP-4T79RA1;Database=EFCoreStudentDb;Trusted_Connection=True;";
- services.AddDbContext < StudentContext > (options => options.UseSqlServer(connection));
- }
First, we have specified the connection info.
- var connection = @"Server=DESKTOP-4T79RA1;Database=EFCoreStudentDb;Trusted_Connection=True;";
After that, we have registered the context using the connection info.
- 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.
- Tools > NuGet Package Manager > Package Manager Console.
- Type Add-Migration Initial to scaffold a migration, press Enter
- Type Update-Database to apply the new migration to the database.
Add-Migration Initial
Update-Database
Now, open SSMS in order to explore our newly created database. As you can see from the below image.
Hope this will help.