2016-06-27 11 views
6

私のドメインは、別個のアセンブリとDbContextの束の間で分割されており、すべて同じ基本Sql Serverデータベースを使用しているプロジェクトがあります。これらのアセンブリは、1つの例外を除いて互いに参照しません。共有エンティティと呼ぶものが含まれています。他のすべてのドメインに共通であり、時にはナビゲーションプロパティとして参照されます。簡単な例:追加マイグレーション中に参照アセンブリからエンティティを無視する

// 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"; 
     } 
    } 
} 

問題は、私はHero POCOを呼び出し、ICollection<Hero> Protagonistsナビゲーションプロパティを介してGame.dllアセンブリモデルで参照しているときに、次のとおりです。

add-migration Test -ProjectName:Game -ConfigurationTypeName MyDbContextConfiguration -StartUpProjectName Main 

があることDbMigrationを作成するに終わります参照されたShared.dll asssemblyからのHeroエンティティに対する変更が含まれています。私はDbContextが定義されているアセンブリに位置エンティティの変更を監視するadd-migrationを制限することができますどのように

public partial class Test : DbMigration 
    { 
     public override void Up() 
     { 
      AddColumn("shared.Heroes", "Name", c => c.String()); 
      AddColumn("shared.Heroes", "Description", c => c.String()); 
      ... 

?つまり、add-migrationに対してGames.dllを実行した場合エンティティに加えられた変更を無視したい場合はShared.dllです。

また、名前空間またはデータベースオブジェクトのスキーマを制限する機能もあります。すべての移行がアセンブリごとに維持されるため、参照されているアセンブリにあるエンティティの変更をマイグレーションに含める必要はありません。あなたは有界コンテキストに精通している場合

答えて

3

私はそれが簡単にあなたのためのトリックを持っていて、(自分のMyDbContextでmodelBuilder.Ignoreを使用して)それを使用することができ、これはあなたのための新しいものではありませんする必要があります:

あなたの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; } 
} 

そして今できることのようなコマンドラインからアドオンの移行:

アドオン移行FirstDb -ConfigurationTypeName設定-CONNECTIONSTRINGNAME YourConnectionStrinグラム


これは、uはあなたの例で使用している同様のエンティティの出力である

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"); 
    } 
} 
関連する問題