2016-06-27 23 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"); 
    } 
} 
相关问题