我有一個項目,我的域在一堆單獨的程序集和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。
什麼也可以工作將是限制命名空間或數據庫對象模式的能力。我只是不希望對位於引用程序集中的實體進行任何更改以包含在我的遷移中,因爲所有遷移均按每個程序集進行維護。