Seb*_*ada 6 .net c# entity-framework entity-framework-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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我通过导航属性Hero在Game.dll程序集模型中引用POCO 时ICollection<Hero> Protagonists,调用:
add-migration Test -ProjectName:Game -ConfigurationTypeName MyDbContextConfiguration -StartUpProjectName Main
Run Code Online (Sandbox Code Playgroud)
最终创建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());
...
Run Code Online (Sandbox Code Playgroud)
如何add-migration仅限制位于已定义DbContext的程序集中的实体的监视更改?换句话说,运行时,add-migration对Games.dll我想忽略来自于实体所做的任何更改Shared.dll.
还可以通过命名空间或数据库对象模式来限制.我只是不希望任何位于引用程序集中的实体的更改都包含在我的迁移中,因为所有迁移都是按程序集维护的.
我有一个技巧给你,它很简单,你可以使用它(通过在你的 MyDbContext 中使用 modelBuilder.Ignore ),如果你熟悉有界上下文,那么这对你来说应该不是什么新鲜事:
您的数据库上下文:
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; }
}
Run Code Online (Sandbox Code Playgroud)
现在您可以从命令行添加迁移,如下所示:
add-migration 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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
227 次 |
| 最近记录: |