Using a Separate Migrations Project

You may want to store your migrations in a different project than the one containing your DbContext

Arison Cao
1 min readJul 14, 2021

In good practice, you often have three projects:

  1. Entities Project which will contain all Entity (or Model) classes. These classes represent the database tables.
  2. DBContext Project help communicates between database and application.
  3. Application App Project uses DBContext.

Setup to using Migration tools on DBContext Project

  1. Implement default constructor in DBContext Project:

public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)

2. In Application Project -> startup.cs -> ConfigureServices

string dataPath =Configuration.GetConnectionString(“DefaultConnection”);

services.AddDbContext<ApplicationContext>(options =>options.UseMySql(dataPath,ServerVersion.AutoDetect(dataPath),b=>b.MigrationsAssembly(typeof(ApplicationContext).Assembly.FullName)));

Add Migration

In Visual Studio 2019:

open Package Manager Console -> in Default Project choose Context Project. In Solution Explorer, right-click on Application Project -> set as startup project.

add-migration Initial

update-database

In Visual Studio Code:

In the terminal, navigate into the Context Project folder

dotnet ef migrations add Initial — —startup-project “../ApplicationProject/ApplicationProject.csproj”

dotnet ef database update — — startup-project “../ApplicationProject/ApplicationProject.csproj”

“../ApplicationProject/ApplicationProject.csproj” : link to Application Project’ project file.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arison Cao
Arison Cao

Written by Arison Cao

IT, Programming, Photographer, Smarthome

No responses yet

Write a response