Using Entity Framework Core migrations for class library project

I reproduced your solution and found... a solution :)

  1. "Core" project - called ClassLibrary1
  2. "Relational" project - called EFClssLibrary
  3. WPF App project - called WpfApp1

Let's get to deepen.


1. Core project

Name: ClassLibrary1.

Type: .NET Standard 2.0 class library.

Dependencies: none.

In my test solution, it contains only one class, a model called Person.

Person.cs

namespace ClassLibrary1
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }
}

2. Relational project

Name: EFClassLibrary.

Type: .NET Standard 2.0 class library.

Dependencies:

  • ClassLibrary1
  • Microsoft.EntityFrameworkCore (v2.1.1)
  • Microsoft.EntityFrameworkCore.SqlServer (v2.1.1)
  • Microsoft.EntityFrameworkCore.Tools (v2.1.1)

This project, in my test solution, contains only one class: the database context.

ClientDbContext.cs

using ClassLibrary1;
using Microsoft.EntityFrameworkCore;

namespace EFClassLibrary
{
    public class ClientDbContext : DbContext
    {
        const string connectionString = "Server=(localdb)\\mssqllocaldb;Database=ClientDb;Trusted_Connection=True;";

        public ClientDbContext() : base() { }

        public ClientDbContext(DbContextOptions<ClientDbContext> options) : base(options) { }

        public DbSet<Person> People { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(connectionString);
        }
    }
}

Connection string

In this class a defined an used the connection string to connect to the database (assuming it's LocalDb SQL Server). If you want to put the connection string in a config file, you could add a shared config file in your solution and then reference that file in your App.config file (for more informartion take a look at this page)

Configuring the target framework

In order to be able to add migrations on this project without setting as startup project other projects, you must set the target framework. Right click on the project and click on the Edit EFClassLibrary.csproj entry. Below the <TargetFramework>netstandard2.0</TargetFramework> line, you should add another line which specify which framework you want to target. To target the .NET Framework 4.7 you should add

<TargetFramework>net47</TargetFramework>

A list of all allowed values can be found here.

My EFClassLibrary.csproj look like the code below after adding the .NET Framework 4.7 as target.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <TargetFramework>net47</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
  </ItemGroup>

</Project>

Adding migrations

Now you are ready to add your first migration. Open the Package Manager Console and set as default project the EFClassLibrary. Also, set that project as startup project (right-click on the project and click on the Set as startup project entry).

Type

PM> Add-Migration Initial

then

PM> Update-Database

3. WPF App project

Name: WpfApp1.

Type: WPF Application using .NET Framework 4.7.

Dependencies:

  • ClassLibrary1
  • EFClassLibrary
  • Microsoft.EntityFrameworkCore (v2.1.1)

In this project I added no files. A just edited the MainWindow.xaml.cs file to check that everything works correctly.

MainWindow.xaml.cs

using ClassLibrary1;
using EFClassLibrary;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var db = new ClientDbContext();

            db.People.Add(new Person()
            {
                Name = "Omar"
            });

            db.SaveChanges();
        }
    }
}

Hope it helps :)


What microsoft suggests here is to create a new class library for the migrations, then move your model snapshots files and migrations to the new class library.

If you haven't added any, add one to the DbContext project then move it.

Then configure migration assembly:

options.UseSqlServer(
    connectionString,
    x => x.MigrationsAssembly("MyApp.Migrations"));

And then add reference to your migrations assembly from your startup assembly:

note:

If this causes a circular dependency, update the output path of the class library:

<PropertyGroup>
  <OutputPath>..\MyStarupProject\bin\$(Configuration)\</OutputPath>
</PropertyGroup>

for a second reference use Ben Cull's guide on EntityFramework Core migration for class library projects.


In your startup project you have to create a factory which will implement IDesignTimeDbContextFactory and create the DBContext for you.

public class DBContextFactory : IDesignTimeDbContextFactory<DBContext>
        {
            public DBContext CreateDbContext(string[] args)
            {
                var optionsBuilder = new DbContextOptionsBuilder<DBContext>();

                // Row numbers for paging adds support for old SQL server 2005+. See more: 
                // https://docs.microsoft.com/en-us/ef/core/api/microsoft.entityframeworkcore.infrastructure.sqlserverdbcontextoptionsbuilder
                optionsBuilder.UseSqlServer("Server=localhost;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=SSPI", x => x.UseRowNumberForPaging());

                return new DBContext(optionsBuilder.Options);
            }
        }

What worked for me is just do your regular stuff on your UI Side Startup.cs

services.AddDbContext<ShentonEvaDbContext>(options =>
                options.UseSqlServer(
                    _configuration.GetConnectionString("DevelopmentConnection")));

Then on your DBContext Configuration just add a constructor

public ShentonEvaDbContext(DbContextOptions<ShentonEvaDbContext> options) : base(options)
        {

        }

After that on your Package Manager Console just run the following

dotnet ef migrations add InitialMigration --project "NameOfDbContextProject" --startup-project "NameOfWebUIProject"

Then Everytihng went added and do the same for the update of database