Skip to content

Documentation on migrations with multiple projects is unclear #933

Closed

Description

@garyapps

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.

  1. 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?

  2. And what does "startup assembly" refer to? The web app or the class library?

  3. 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.

  4. 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.

Activity

  1. mloffer commented on Aug 30, 2018

    @mloffer

    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?

  2. famoser commented on Oct 8, 2018

    @famoser

    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 (contains MyDbContext & entities)
    • Migrations (contains Migrations; references Data)
    • 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();
    	}
    }
    
  3. added
    help wanted
    on Nov 5, 2018
  4. fizxmike commented on Nov 7, 2018

    @fizxmike

    ... 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();?

  5. famoser commented on Nov 7, 2018

    @famoser

    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.

  6. fizxmike commented on Nov 7, 2018

    @fizxmike

    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...

  7. asad-c commented on Nov 10, 2018

    @asad-c

    The documentation is too brief, but I've managed to get it working as follows (whilst also adhering to Onion Architecture):

    1. 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
    1. 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'

    1. 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'

    1. 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:

    <PropertyGroup>
    	<TargetFramework>netcoreapp2.1</TargetFramework>
    	<OutputPath>App.Infrastructure\bin\$(Configuration)\</OutputPath>
    </PropertyGroup>
    
    • 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

    1. Build the solution

    2. 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

    3. 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

    1. Remember to rebuild the solution every time a migration is added
  8. added this to the 2.2.0 milestone on Nov 10, 2018
  9. removed
    help wanted
    on Nov 10, 2018
  10. changed the title [-]Documentation unclear, suggestions for improvement[/-] [+]Documentation on migrations with multiple projects unclear[/+] on Nov 10, 2018
  11. changed the title [-]Documentation on migrations with multiple projects unclear[/-] [+]Documentation on migrations with multiple projects is unclear[/+] on Nov 10, 2018
  12. added
    help wanted
    on Feb 21, 2019
  13. modified the milestones: 2.2.0, 3.0.0 on Feb 21, 2019
  14. 39 remaining items

  15. added this to the 7.0.0 milestone on Nov 3, 2021
  16. dgxhubbard commented on Dec 9, 2021

    @dgxhubbard

    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" ).

  17. defense89 commented on Feb 10, 2022

    @defense89

    The documentation is too brief, but I've managed to get it working as follows (whilst also adhering to Onion Architecture):

    1. 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
    1. 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'
    1. 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'
    1. 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:
    <PropertyGroup>
    	<TargetFramework>netcoreapp2.1</TargetFramework>
    	<OutputPath>App.Infrastructure\bin\$(Configuration)\</OutputPath>
    </PropertyGroup>
    
    • 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
    1. Build the solution
    2. 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
    3. 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
    1. 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.

    probmig

  18. thomas-le-goff commented on Nov 3, 2022

    @thomas-le-goff

    Hello,
    For those who are looking for a solution to put Migrations in different assemblies here is a little helper I wrote: https://gist.github.com/thomas-le-goff/a6b735efccff34230ceb3ca11a2d9cb4

    I 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 😉.

  19. removed their assignment
    on Jul 8, 2023
  20. removed their assignment
    on Aug 31, 2024
  21. stevecwilkinson commented on Feb 13, 2025

    @stevecwilkinson

    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.

    I'm posting a comment here because the documentation at https://learn.microsoft.com/en-us/ef/core/cli/dotnet for the script command implies it should, as it states in that section "The common options are listed above.", of which --project is 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.

  22. Ptipoi-jf commented on Dec 16, 2025

    @Ptipoi-jf

    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 :

    1. cleaning the whole project
    2. generate project migrations
    3. generate my app (again, already built with migration build) -- required to create X..deps.json and X.runtimeconfig.json files
    4. 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 :

    1. rebuild unit test project
      or
    2. rebuild the solution

    Rebuild the solution is not working because step 3 is not "done", X..deps.json and X.runtimeconfig.json are missing

    cheers

  23. added a commit that references this issue on Aug 11, 2026
    bb42937
  24. modified the milestones: 7.0.0, 11.0.0 on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions