Ignoring objects from referenced assemblies during add-migration

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.

+6
source share
1 answer

I have a trick for you that is easy and you can use it (using ModelBuilder.Ignore in MyDbContext), if you are familiar with a limited context, then this should not be something new for you:

Your DbContext:

  public class MyDbContext : DbContext { private readonly bool _isMigrationMode; public MyDbContext() { // This used by migration default and you can give the connection string in the command line. _isMigrationMode = true; } // isMigrationMode: I have give it here as an optional parameter, in case, if you want to create the migration from the code. public MyDbContext(string connectionString, bool isMigrationMode = false) : base("name=" + connectionString) { _isMigrationMode = isMigrationMode; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { if (_isMigrationMode) { modelBuilder.Ignore<Hero>(); } base.OnModelCreating(modelBuilder); } public DbSet<Mission> Missions { get; set; } } 

And now you can add-transfer from the command line as follows:

add-migration FirstDb -ConfigurationTypeName Configuration -CONNECTIONSTRINGNAME YourConnectionString


This is the result for a similar object that you used in your example

  public partial class FirstDb : DbMigration { public override void Up() { CreateTable( "dbo.Missions", c => new { MissionId = c.Long(nullable: false, identity: true), Amount = c.Int(nullable: false), Amount2 = c.Int(nullable: false), HeroId = c.Long(nullable: false), }) .PrimaryKey(t => t.MissionId); } public override void Down() { DropTable("dbo.Missions"); } } 
+3
source

All Articles