I have a project where my domain is split between a bunch of separate assemblies and DbContexts, all of which use the same Sql Server core database. These assemblies do not refer to each other with one exception - there is one that contains what might be called shared objects that are common to all other domains and are sometimes referred to as navigation properties. A simplified example:
// Shared.dll namespace Shared { // Shared POCO class Hero { public string Name { get; set; } public string Description { get; set; } } class MyDbContext : DbContext { public virtual DbSet<Hero> Heroes { get; set; } } internal sealed class MyDbContextConfiguration : DbMigrationsConfiguration<MyDbContext> { public MyDbContextConfiguration () { AutomaticMigrationsEnabled = false; MigrationsDirectory = @"Migrations\MyDbContext"; ContextKey = "Shared"; } } } // Game.dll <- references Shared.dll namespace Game { // Individual POCO class Mission { public string Name { get; set; } public virtual ICollection<Hero> Protagonists { get; set; } } class MyDbContext : DbContext { public virtual DbSet<Mission> Missions { get; set; } } internal sealed class MyDbContextConfiguration : DbMigrationsConfiguration<MyDbContext> { public MyDbContextConfiguration () { AutomaticMigrationsEnabled = false; MigrationsDirectory = @"Migrations\MyDbContext"; ContextKey = "Game"; } } }
The problem is that when I have Hero POCO, a link to the Game.dll assembly model through the navigation property ICollection<Hero> Protagonists , calling:
add-migration Test -ProjectName:Game -ConfigurationTypeName MyDbContextConfiguration -StartUpProjectName Main
ends with the creation of DbMigration, which includes changes to the Hero object from the referenced assembly Shared.dll .
public partial class Test : DbMigration { public override void Up() { AddColumn("shared.Heroes", "Name", c => c.String()); AddColumn("shared.Heroes", "Description", c => c.String()); ...
How can I limit add-migration monitoring changes only for objects located in the assembly where the DbContext was defined? In other words, when running add-migration against Games.dll, I want to ignore any changes made to objects from Shared.dll .
What may also work is the possibility of restricting the design of a namespace or database objects. I just donβt want any changes to the objects located in the referenced assemblies to be included in my migrations, since all migrations are supported in each assembly.
source share