This seem to be an issue that have been fixed already, at least for the SQLite databases.
My solution consists of 3 projects:
- WPF project (default startup project) (.NET Framework 4.7),
- "Core" project holding the view model and non-UI stuff - Class library project (.NET Standard 2.0)
- "Relational" project holding all of the Entity Framework data layer - I like to keep those separated (.NET Standard 2.0)
I have installed the following packages into the main WPF project:
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design
Projects 2 and 3 are referenced in my main WPF project. So basically, it should be enough for the EF to resolve the DbContextes.
However, it's not - as running Add-Migration
on my WPF project results in:
PM> Add-Migration "Initial"
No DbContext was found in assembly 'TestWPFProject'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
Switching to project 3
as default in the Package Manager Console results in:
PM> Add-Migration "Initial"
Unable to create an object of type 'ClientDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ClientDataStoreDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
How can I properly use EF Core migrations with my class library project and WPF project?
I was using class library project as data layer and asp.net core project as start up project.First I registered my E.F core context in asp.net start up class.
Then in my context class(located in class library project) I provided connection string from setting file and used it in OnConfiguring method.
then for applying migration and updating database I used following commands respectively
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:
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:
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.
What worked for me is just do your regular stuff on your UI Side Startup.cs
Then on your DBContext Configuration just add a constructor
After that on your Package Manager Console just run the following
Then Everytihng went added and do the same for the update of database
I reproduced your solution and found... a solution :)
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
2. Relational project
Name: EFClassLibrary.
Type: .NET Standard 2.0 class library.
Dependencies:
This project, in my test solution, contains only one class: the database context.
ClientDbContext.cs
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 addA 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.
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
then
3. WPF App project
Name: WpfApp1.
Type: WPF Application using .NET Framework 4.7.
Dependencies:
In this project I added no files. A just edited the
MainWindow.xaml.cs
file to check that everything works correctly.MainWindow.xaml.cs
Hope it helps :)