是否可以修改/替换在不同上下文中继承的上下文中的实体配置?
示例:我在名为框架的解决方案中有一个名为Data.Access的项目的上下文。它的OnModelCreating函数通过如下方式添加实体配置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// TestEntityConfiguration is the configuration of an entity named TestEntity in the Framework solution
modelBuilder.Configurations.Add(new TestEntityConfiguration());
// multiple other configurations...
}在另一个名为FrameworkConsumer的解决方案中,我有一个Local.Data.Access项目,它有一个Context类,它扩展了框架解决方案中来自Data.Access的上下文。它的OnModelCreating函数如下所示:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Adds all the configurations from the Context in Data.Access in the Framework solution
base.OnModelCreating(modelBuilder);
// Other configurations local to the extended context go here...
}我的问题是。在FrameworkConsumer解决方案的Local.Data.Access项目中,如果我想为TestEntity添加额外的配置设置或不同的配置,该如何实现?我尝试添加另一个配置,但是,我收到错误消息,指出此实体(TestEntity)已被配置。现在,我添加额外配置的解决方案是在Local.Data.Access上下文类的Dispose函数中使用Database.ExecuteSqlCommand。不是很优雅,但它很管用。任何想法/建议都将不胜感激。
谢谢
发布于 2013-03-13 02:02:03
您可以将该配置放在另一个虚拟方法中,如果您需要更改它,则可以覆盖该方法。例如:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// multiple other configurations...
SpecialConfigurations(modelBuilder);
}
protected virtual void SpecialConfigurations(DbModelBuilder modelBuilder)
{
// TestEntityConfiguration is the configuration of an entity named TestEntity in the Framework solution
modelBuilder.Configurations.Add(new TestEntityConfiguration());
// multiple other configurations...
}然后重写SpecialConfigurations方法。
https://stackoverflow.com/questions/13253497
复制相似问题