我有一个项目,其中我的域被分成一堆单独的程序集和 DbContext,它们都使用相同的底层 Sql Server 数据库。除了一个异常(exception),这些程序集不会相互引用 - 有一个程序集包含可能称为共享实体的东西,这些实体对所有其他域都是通用的,有时被称为导航属性。简化示例:
// 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
通过ICollection<Hero> Protagonists
在Game.dll 装配模型中引用POCO导航属性,调用:
add-migration Test -ProjectName:Game -ConfigurationTypeName MyDbContextConfiguration -StartUpProjectName Main
最终创建了包含对 Hero
的更改的 DbMigration来自引用的 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());
...
如何限制add-migration
仅监视位于已定义 DbContext 的程序集中的实体的更改?换句话说,当运行 add-migration
针对 Games.dll 我想忽略对 Shared.dll 中的实体所做的任何更改。
还可以工作的是通过 namespace 或数据库对象架构进行限制的能力。我只是不希望将位于引用程序集中的实体的任何更改包含在我的迁移中,因为所有迁移都是按程序集维护的。
最佳答案
我有一个简单的技巧供您使用(通过在 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 YourConnectionString
这是您在示例中使用的类似实体的输出
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");
}
}
关于c# - 在添加迁移期间忽略来自引用程序集的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38052413/