You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Documentation on migrations with multiple projects is unclear #933
I agree with the other commenter that this article is not very clear. I'd go so far as to say it is pretty badly written.
The code snippet refers to "MyApp.Migrations". What is MyApp? Is that the name of the original web project (with dbcontext), or is it the new class library?
And what does "startup assembly" refer to? The web app or the class library?
The fix for removing the circular reference does not work. If each project is supposed to reference the other, then we should expect a circular reference, which is what we get. The suggested fix does not appear to work (not sure if I am doing it wrong), and since no part of the article explains the reasoning behind any of the steps, one cannot even troubleshoot if these steps didn't work.
It would also help to include a few screenshots of how to add a new class library, how to add a reference, and where one can find the XML file where the circular reference fix is supposed to go. After all, we are not all Visual Studio experts.
I have been unable to successfully put migrations in a separate assembly, so I would say this article has not helped me at all.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: b545eeff-6c33-3cc0-09ce-a3ad342d7438
Version Independent ID: 34bc1bac-7338-d8ab-390d-8c5e88a48c10
Number 3 says Move the migrations and model snapshot files to the class library.
Do I just move the files, or do I also need to change the namespace?
Number 4 says Configure the migrations assembly.
But the code listed after it looks like code I would expect to see in the original DbContext assembly. Where does this go in the Migration assembly? How about a filename?
Reacted by Bekim Gjokaj, Dan Elliott, Andy, br3nt, Elias Cooper, Jorge Morales, Ing. Lukas Josai, Jason Vincent and Suman Lama
App (contains startable .Net Core app, references Data + Migrations)
Test (contains unit tests, references App + Data + Migrations)
Usecase:
I want to use same migrations for both App & Test.
How to prepare:
Create projects & link appropriately
execute dotnet ef migrations add InitialMigration --startup-project ../App/ inside the Migrations folder
in the App, you need to initialize MyDbContext as follows:
services.AddDbContext<MyDbContext>(options => options.UseSqlite(connection, x => x.MigrationsAssembly("Migrations")));
Now you can
execute dotnet ef database update inside the App folder which upgrades the App database
use the MyDbContext in your unit tests, applying migrations manually
Unit Test example code:
[TestClass]
public class MyUnitTest
{
[ClassInitialize]
public static void SetUp(TestContext testContext)
{
//setup DI
var services = new ServiceCollection();
services.AddDbContext<MyDbContext>(options => options.UseSqlite("DataSource=test.sqlite", x => x.MigrationsAssembly("Migrations")));
var serviceCollection = services.BuildServiceProvider();
//migrate db
var context = serviceCollection.GetService<MyDbContext>();
context.Database.Migrate();
}
}
Reacted by Alex J., Mahesh Chauhan, Jorge Morales, Mohammed Elghamry, Sudara Fernando, Georgi Todorov, kelwinssavoia, Wisdou and Pat Mulex
... but how to migrate with continuous delivery (like in Azure DevOps)? Deployment tasks typically access build artifacts only. Is there a way to run migrations after build? Or do I need to include source in the artifacts and add dotnet SDK to task environment? Is dotnet ef database update equivalent to myDbContext.Database.Migrate();?
You can run any commands in the post deployment script of your release task. I did not need any special configuration (I am using a release pipeline which Visual Studio setup automatically). I do not know if its completely equivalent, but I've used both and did not run into any issues.
Azure DevOps is a bit different. The "release" pipelines begin with artifacts (from builds) only. Another complication is I'm doing a self-contained deployment (no dotnet core runtime on target machines), I essentially only have access to the compiled binaries (.exe and dll) at deploy time. I ended up adding a stage in my pipeline to call the .exe with a command line argument which triggers context.Database.Migrate(). I'm hoping this has same behavior as dotnet ef database update. What I'm seeing so far is it is "silent" (no output). Hmm...
The documentation is too brief, but I've managed to get it working as follows (whilst also adhering to Onion Architecture):
Create a class library 'App.Core'
Add a 'Country' domain model to this library:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
This is just a simple class to get things working quickly
Create a class library 'App.Infrastructure'
Add a DbContext to this library:
public class AppDbContext : DbContext
{
public DbSet<Country> Countries { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=localhost;Database=App;Trusted_Connection=True;",
x => x.MigrationsAssembly("App.Migrations"));
}
}
'App.Migrations' will be our separate class library just for migrations
'App.Infrastructure' needs to reference 'Microsoft.EntityFrameworkCore.SqlServer' and 'Microsoft.EntityFrameworkCore.Tools'
Run dotnet ef migrations add InitialCreate
Do this from the command line in the 'App.Infrastructure' directory
This will create a 'Migrations' folder in your 'App.Infrastructure' class library with a migration called 'InitialCreate'
Create a class library 'App.Migrations'
Move the 'Migrations' folder from 'App.Infrastructure' to 'App.Migrations' - you will need to update the namespaces after the move
Add a project reference in 'App.Migrations' to 'App.Infrastructure'
Edit the .csproj file for 'App.Migrations' and add an output path:
The above path will be correct if 'App.Infrastructure' and 'App.Migrations' are in the same directory, if not, the output path will need to be adjusted
On build this results in 'App.Migrations' being output to the 'App.Infrastructure' bin directory - we need to do this as we can't reference 'App.Migrations' in 'App.Infrastructure' as this results in a circular reference, so this is a workaround
Build the solution
Run dotnet ef database update from the command line in the 'App.Infrastructure' directory and this should create the database and create the 'Countries' table
Run dotnet ef migrations add AddCity --project App.Migrations for your next migration
'AddCity' is just another migration to create a 'Cities' table - this requires adding a 'City' class and updating the DbContext
Run the command from the 'App.Infrastructure' directory and the migration will be added to the 'App.Migrations' class library
Remember to rebuild the solution every time a migration is added
Reacted by Alex Manea, Sudara Fernando, Evan BOISSONNOT and Nicolas Parisse
changed the title [-]Documentation unclear, suggestions for improvement[/-][+]Documentation on migrations with multiple projects unclear[/+]on Nov 10, 2018
changed the title [-]Documentation on migrations with multiple projects unclear[/-][+]Documentation on migrations with multiple projects is unclear[/+]on Nov 10, 2018
I think this issue #3152 should re-opened, trying to follow the docs which seem to assume most people are running migrations from CLI command line, does not work for the case of context.Database.Migrate. I have done the separate solutions for our different providers and generated the migrations, only to find context.Migrate.Database cannot find the migrations assembly specified in UseMyProvider ( connStr, x => x.MigrationsAssembly ( "MyProviderMigrations" ).
The documentation is too brief, but I've managed to get it working as follows (whilst also adhering to Onion Architecture):
Create a class library 'App.Core'
Add a 'Country' domain model to this library:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
This is just a simple class to get things working quickly
Create a class library 'App.Infrastructure'
Add a DbContext to this library:
public class AppDbContext : DbContext
{
public DbSet<Country> Countries { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=localhost;Database=App;Trusted_Connection=True;",
x => x.MigrationsAssembly("App.Migrations"));
}
}
'App.Migrations' will be our separate class library just for migrations
'App.Infrastructure' needs to reference 'Microsoft.EntityFrameworkCore.SqlServer' and 'Microsoft.EntityFrameworkCore.Tools'
Run dotnet ef migrations add InitialCreate
Do this from the command line in the 'App.Infrastructure' directory
This will create a 'Migrations' folder in your 'App.Infrastructure' class library with a migration called 'InitialCreate'
Create a class library 'App.Migrations'
Move the 'Migrations' folder from 'App.Infrastructure' to 'App.Migrations' - you will need to update the namespaces after the move
Add a project reference in 'App.Migrations' to 'App.Infrastructure'
Edit the .csproj file for 'App.Migrations' and add an output path:
The above path will be correct if 'App.Infrastructure' and 'App.Migrations' are in the same directory, if not, the output path will need to be adjusted
On build this results in 'App.Migrations' being output to the 'App.Infrastructure' bin directory - we need to do this as we can't reference 'App.Migrations' in 'App.Infrastructure' as this results in a circular reference, so this is a workaround
Build the solution
Run dotnet ef database update from the command line in the 'App.Infrastructure' directory and this should create the database and create the 'Countries' table
Run dotnet ef migrations add AddCity --project App.Migrations for your next migration
'AddCity' is just another migration to create a 'Cities' table - this requires adding a 'City' class and updating the DbContext
Run the command from the 'App.Infrastructure' directory and the migration will be added to the 'App.Migrations' class library
Remember to rebuild the solution every time a migration is added
talking about a tutorial!!!!! you got it man a step by step for beginners your awesome.... i get an error however while running dotnet ef migrations add initial create with code , could not find librairy app.migration , i tried to add it but still not working.
I added the following comment before I realised the issue:
"I raised feature request issue #35637 earlier as I thought that --project wasn't being applied in the case of the dotnet ef migrations has-pending-model-changes command, but now I believe it doesn't get applied when you run dotnet ef migrations script command either.
Does anyone else have experience generating scripts when using separate migration assemblies? Sadly all the examples for separate migration assemblies stop at the dotnet ef migrations add ... stage."
The issue is that you must build the migration projects before you run either command. It would be really helpful if this were added to the documentation for working with separate migration assemblies.
Any news ? i remenber this topic was in progress... but nothing has been done
here we are with net10 version and no changes, my projects builds are not stable because of "you must build migration project before the main project".
BUT with net10 (or vs2026 ?), an other problem appear : when rebuild my test app project, X..deps.json and X.runtimeconfig.json are not found, so my unit test discovery did not work.
My unit test reference project migration and project migration reference my app. adding my app reference to my unit test did not solve the problem.
what i need to do is :
cleaning the whole project
generate project migrations
generate my app (again, already built with migration build) -- required to create X..deps.json and X.runtimeconfig.json files
generate unit test project
my unit test MAY not work if theses steps are not done in this order...
what i want to be able to do :
rebuild unit test project
or
rebuild the solution
Rebuild the solution is not working because step 3 is not "done", X..deps.json and X.runtimeconfig.json are missing
Activity
mloffer commentedon Aug 30, 2018
Seconded.
Number 3 says Move the migrations and model snapshot files to the class library.
Do I just move the files, or do I also need to change the namespace?
Number 4 says Configure the migrations assembly.
But the code listed after it looks like code I would expect to see in the original DbContext assembly. Where does this go in the Migration assembly? How about a filename?
famoser commentedon Oct 8, 2018
With the help of https://github.com/bricelam/Sample-SplitMigrations I could create a setup which allows to use the same migrations for both my App and my unit tests. It is what I wanted to accomplish when reading the documentation, hopefully this usecase helps someone else; and possibly when enhancing the documentation.
Setup:
Data(containsMyDbContext& entities)Migrations(contains Migrations; referencesData)App(contains startable .Net Core app, referencesData+Migrations)Test(contains unit tests, referencesApp+Data+Migrations)Usecase:
I want to use same migrations for both
App&Test.How to prepare:
dotnet ef migrations add InitialMigration --startup-project ../App/inside theMigrationsfolderApp, you need to initializeMyDbContextas follows:Now you can
dotnet ef database updateinside theAppfolder which upgrades theAppdatabaseMyDbContextin your unit tests, applying migrations manuallyUnit Test example code:
fizxmike commentedon Nov 7, 2018
... but how to migrate with continuous delivery (like in Azure DevOps)? Deployment tasks typically access build artifacts only. Is there a way to run migrations after build? Or do I need to include source in the artifacts and add dotnet SDK to task environment? Is
dotnet ef database updateequivalent tomyDbContext.Database.Migrate();?famoser commentedon Nov 7, 2018
You can run any commands in the post deployment script of your release task. I did not need any special configuration (I am using a release pipeline which Visual Studio setup automatically). I do not know if its completely equivalent, but I've used both and did not run into any issues.
fizxmike commentedon Nov 7, 2018
Azure DevOps is a bit different. The "release" pipelines begin with artifacts (from builds) only. Another complication is I'm doing a self-contained deployment (no dotnet core runtime on target machines), I essentially only have access to the compiled binaries (.exe and dll) at deploy time. I ended up adding a stage in my pipeline to call the .exe with a command line argument which triggers context.Database.Migrate(). I'm hoping this has same behavior as
dotnet ef database update. What I'm seeing so far is it is "silent" (no output). Hmm...asad-c commentedon Nov 10, 2018
The documentation is too brief, but I've managed to get it working as follows (whilst also adhering to Onion Architecture):
'App.Migrations' will be our separate class library just for migrations
'App.Infrastructure' needs to reference 'Microsoft.EntityFrameworkCore.SqlServer' and 'Microsoft.EntityFrameworkCore.Tools'
dotnet ef migrations add InitialCreateDo this from the command line in the 'App.Infrastructure' directory
This will create a 'Migrations' folder in your 'App.Infrastructure' class library with a migration called 'InitialCreate'
Move the 'Migrations' folder from 'App.Infrastructure' to 'App.Migrations' - you will need to update the namespaces after the move
Add a project reference in 'App.Migrations' to 'App.Infrastructure'
Edit the .csproj file for 'App.Migrations' and add an output path:
The above path will be correct if 'App.Infrastructure' and 'App.Migrations' are in the same directory, if not, the output path will need to be adjusted
On build this results in 'App.Migrations' being output to the 'App.Infrastructure' bin directory - we need to do this as we can't reference 'App.Migrations' in 'App.Infrastructure' as this results in a circular reference, so this is a workaround
Build the solution
Run
dotnet ef database updatefrom the command line in the 'App.Infrastructure' directory and this should create the database and create the 'Countries' tableRun
dotnet ef migrations add AddCity --project App.Migrationsfor your next migration'AddCity' is just another migration to create a 'Cities' table - this requires adding a 'City' class and updating the DbContext
Run the command from the 'App.Infrastructure' directory and the migration will be added to the 'App.Migrations' class library
[-]Documentation unclear, suggestions for improvement[/-][+]Documentation on migrations with multiple projects unclear[/+][-]Documentation on migrations with multiple projects unclear[/-][+]Documentation on migrations with multiple projects is unclear[/+]39 remaining items
dgxhubbard commentedon Dec 9, 2021
I think this issue #3152 should re-opened, trying to follow the docs which seem to assume most people are running migrations from CLI command line, does not work for the case of context.Database.Migrate. I have done the separate solutions for our different providers and generated the migrations, only to find context.Migrate.Database cannot find the migrations assembly specified in UseMyProvider ( connStr, x => x.MigrationsAssembly ( "MyProviderMigrations" ).
defense89 commentedon Feb 10, 2022
talking about a tutorial!!!!! you got it man a step by step for beginners your awesome.... i get an error however while running dotnet ef migrations add initial create with code , could not find librairy app.migration , i tried to add it but still not working.
thomas-le-goff commentedon Nov 3, 2022
Hello,
For those who are looking for a solution to put
Migrationsin different assemblies here is a little helper I wrote: https://gist.github.com/thomas-le-goff/a6b735efccff34230ceb3ca11a2d9cb4I simply documented the steps that took me to a working solution. Maybe it can help until the documentation is suitable for a beginner like me 😉.
stevecwilkinson commentedon Feb 13, 2025
I added the following comment before I realised the issue:
"I raised feature request issue #35637 earlier as I thought that
--projectwasn't being applied in the case of thedotnet ef migrations has-pending-model-changescommand, but now I believe it doesn't get applied when you rundotnet ef migrations scriptcommand either.I'm posting a comment here because the documentation at https://learn.microsoft.com/en-us/ef/core/cli/dotnet for the
scriptcommand implies it should, as it states in that section "The common options are listed above.", of which--projectis one.Does anyone else have experience generating scripts when using separate migration assemblies? Sadly all the examples for separate migration assemblies stop at the
dotnet ef migrations add ...stage."The issue is that you must build the migration projects before you run either command. It would be really helpful if this were added to the documentation for working with separate migration assemblies.
Ptipoi-jf commentedon Dec 16, 2025
Any news ? i remenber this topic was in progress... but nothing has been done
here we are with net10 version and no changes, my projects builds are not stable because of "you must build migration project before the main project".
BUT with net10 (or vs2026 ?), an other problem appear : when rebuild my test app project,
X..deps.jsonandX.runtimeconfig.jsonare not found, so my unit test discovery did not work.My unit test reference project migration and project migration reference my app. adding my app reference to my unit test did not solve the problem.
what i need to do is :
X..deps.jsonandX.runtimeconfig.jsonfilesmy unit test MAY not work if theses steps are not done in this order...
what i want to be able to do :
or
Rebuild the solution is not working because step 3 is not "done",
X..deps.jsonandX.runtimeconfig.jsonare missingcheers
Expand and clarify migrations guidance. (dotnet#5445)