Raymond Raymond / .NET Programming

EntityFramework Core: Generate Migration Scripts in a Separate Project

event 2021-05-15 visibility 710 comment 5 insights toc
more_vert
insights Stats
toc Table of contents

In entity-framework code-first solution, we usually generate migrations scripts in the default startup project. For instance, for ASP.NET Core applications, the scripts can be generated in Migrations folder of the web application project. This articles shows an approach to generate migration scripts in a separate project.

Scenario

Assuming the following solution setup:

sln
      WebProject
      DataModels
      Migrations

In solution folder sln, there are three projects: 

  • WebProject: ASP.NET Core web application project.
  • DataModels: all the entity framework models and application DbContext classes are defined here.
  • Migrations: all the migration scripts.

The task is to generate migration scripts in Migrations project folder. 

Resolution

The following steps provide one solution to implement the above task.

  1. Open Command Prompt (Windows) or Terminal (Linux).
  2. Change current folder to sln:
    cd sln
  3. Add initial migration script:
    dotnet ef migrations add InitMigrations --startup-project WebProject

    The above command will generate initial migration scripts by default in WebProject. The startup project is set as WebProject

  4. Create Migrations project as a C# library project with all the necessary references to entity framework package and DataModels project.
  5. Move the generated scripts to project Migrations manually.
  6. Generate new migrations using the following command ongoing forward:
    dotnet ef migrations add MyNewMigration --startup-project WebProject --project Migrations

References

Using a Separate Migrations Project - EF Core | Microsoft Docs

More from Kontext
comment Comments
J Jaczura Zoltán #1899 access_time 2 months ago more_vert

Thank you!


Everything is fine!


Your blog helped me a lot!


You are awesome!

Raymond Raymond #1898 access_time 2 months ago more_vert

I've added examples for your reference: tang2087/dotnetcore-examples: .NET Core example codes for https://kontext.tech/ (github.com)


Commits ยท tang2087/dotnetcore-examples (github.com)

Please pay attention to the following three commits which represent the three steps I mentioned in the article above:


2024051920507-image.png



format_quote

person Jaczura access_time 2 months ago

Contains means added as project reference.

J Jaczura Zoltán #1897 access_time 2 months ago more_vert

Contains means added as project reference.

format_quote

person Raymond access_time 2 months ago

Can you please clarify 'contains'? the DataModels itself is a classlib project and both webproject and migrations projects reference it (but not contain).

Raymond Raymond #1896 access_time 2 months ago more_vert

Can you please clarify 'contains'? the DataModels itself is a classlib project and both webproject and migrations projects reference it (but not contain).

format_quote

person Jaczura access_time 2 months ago

I created this project structure but if WebProject contains DataModels and Migrations contains DataModels, by creating migrations with the command why it says to me that Could not load assembly 'Migrations'. Ensure it is referenced by the startup project 'WebProject '.

J Jaczura Zoltán #1895 access_time 2 months ago more_vert

I created this project structure but if WebProject contains DataModels and Migrations contains DataModels, by creating migrations with the command why it says to me that Could not load assembly 'Migrations'. Ensure it is referenced by the startup project 'WebProject '.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts