2016-06-27 22 views
6

我有一個項目,我的域在一堆單獨的程序集和DbContexts之間進行了拆分,所有程序都使用相同的底層Sql Server數據庫。這些程序集不會互相引用 - 有一個程序包含可能調用共享實體的內容,這些內容對所有其他域都是共同的,有時也稱爲導航屬性。簡單的例子:在添加遷移過程中忽略引用程序集中的實體

// 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結束了這包括Hero實體與引用的Shared.dll asssembly的更改。

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只爲位於大會實體的變化進行監測,其中的DbContext已被定義?換句話說,當運行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"); 
    } 
} 
相關問題