Entity Framework Core Integration in .NET Core

Create a class library named NetCoreDemo.EF and reference the packages Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer.Design, Microsoft.EntityFrameworkCore.SqlServer, and Microsoft.EntityFrameworkCore.Tools.

Method 1:

Tools > NuGet Package Manager > Package Manager Console (the default project must be NetCoreDemo.EF)

Use the following command:

Scaffold-DbContext -Connection "Server=127.0.0.1;Database=NetCoreDemo;Integrated Security=False;User ID=sa;Password=123456" -Provider "Microsoft.EntityFrameworkCore.SqlServer" -OutputDir Models

This generates classes corresponding to database tables, stored within the Models folder.

Method 2:

Replace Microsoft.EntityFrameworkCore.SqlServer.Design with Microsoft.EntityFrameworkCore.Design among the four packages.

Manually add the context class:

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }

    public DbSet<Blog> Blog { get; set; }
    public DbSet<Post> Post { get; set; }
}

Add entity classes:

[Table("Blog")]
public class Blog
{
    [Key]
    public int BlogId { get; set; }
    public string Url { get; set; }
    public virtual List<Post> Posts { get; set; }
}

[Table("Post")]
public class Post
{
    [Key]
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

In the main project's Startup.cs file, within ConfigureServices, add:

#region Use EF
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<MyDbContext>(options =>
    options.UseSqlServer(connectionString));
#endregion

Add the connection string in appsettings.json:

"ConnectionStrings": {
    "DefaultConnection": "Data Source=.;Initial Catalog=Blog;Integrated Security=True"
  },

Ensure the main project includes Microsoft.EntityFrameworkCore.Design.

The EF class library is only responsible for defining the DbContext class.

To define the connection string within the EF class library, use the following declaration:

public class MyDbContext : DbContext
{
    private readonly string connStr = "Data Source=.;Initial Catalog=Blog;Integrated Security=True";

    protected override void OnConfiguring(DbContextOptionsBuilder optionBuilder) 
    {
        optionBuilder.UseSqlServer(connStr);
    }

    public DbSet<UserInfo> UserInfo { get; set; }
}

[Code First Migration]

Using Method 1, open the console (ensure the startup project is the one declaring MyDbContext)

Run the following command:

Add-Migration FirstMigration

This creates a Migrations folder in the EF project.

Run the following command:

update-database FirstMigration

The migration name should match between both commands.

Ensure the versions of the four packages are consistent to avoid errors.

Tags: efcore .NET Core database migration Entity Framework

Posted on Wed, 08 Jul 2026 16:19:46 +0000 by affluent980