Introduction:

In this article we are going to explain how to Code First Migration in ASP.NET Core MVC  With Entity Framework Core application in visual studio 2017 with example.

Description:

 We will just create a simple ASP.NET MVC Core web application.

For this, follow these steps in Visual Studio 2017.

  1. File > New > Project.
  2. Select ASP.NET Core Web Application.
  3. Set name and location
  4. In next screen, select Web Application (Model –>View –> Controller) Template and click OK.

Now, add some necessary NuGet packages.

Tools - NuGet Package Manager - Package Manager Console

Run Install-Package Microsoft.EntityFrameworkCore.SqlServer

For Entity Framework Core Tools to create a database from your EF Core model:

Run Install-Package Microsoft.EntityFrameworkCore.Tools

And for ASP.NET Core Scaffolding tools to create Controllers and Views:

Run Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design

 

 

In previous versions of ASP.NET, we often used web.config for connection strings but in ASP.NET Core, we cannot use web.config for this purpose; rather, we can use other techniques or methods for it. Some of the simple methods are given below.

 

Use Connection string from appsettings.json

{
  "Logging":{
    "IncludeScopes": false,
    "LogLevel":{
      "Default":"Warning"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=(local); Database=ArticleDB;user id=sa;
password=sa@123;Trusted_Connection=True; MultipleActiveResultSets=true
"
  }
}

Add class “User.cs” as shown below:

public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string UserEmail { get; set; }
        public string UserContact{ get; set; }
        public string UserAddress{ get; set; }
    }

Add class “DataContext.cs” as shown below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MvcCore.Models;
namespace MvcCore
{
    public class DataContext:DbContext
    {
        public DataContext(DbContextOptions<DataContext> options)
            : base(options)
        { }
 
        public DbSet<User> User { get; set; }
    }
}

 

Add fallowing code to Startup.cs file

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();          
            services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        }

Add two using statements in Startup.cs file

using Microsoft.EntityFrameworkCore;
using MvcCore.Models;

In Tools --> NuGet Package Manager --> Package Manager Console, run two commands;

Add-Migration InitialCreate
Update-Database

This will create our Database using Entity Framework Core.

 

 Using Code First Migration in ASP.NET Core MVC  With Entity Framework Core

I hope you understand now how to Code First Migration in ASP.NET Core MVC  With Entity Framework Core  App using .Net Core 2.0 and Visual Studio 2017.

Please share your valuable feedback or comments to improve my future articles.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like