首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Code first EntityConfiguration

Code first EntityConfiguration
EN

Stack Overflow用户
提问于 2012-11-06 22:49:43
回答 1查看 236关注 0票数 3

是否可以修改/替换在不同上下文中继承的上下文中的实体配置?

示例:我在名为框架的解决方案中有一个名为Data.Access的项目的上下文。它的OnModelCreating函数通过如下方式添加实体配置:

代码语言:javascript
复制
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函数如下所示:

代码语言:javascript
复制
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。不是很优雅,但它很管用。任何想法/建议都将不胜感激。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-03-13 02:02:03

您可以将该配置放在另一个虚拟方法中,如果您需要更改它,则可以覆盖该方法。例如:

代码语言:javascript
复制
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方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13253497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档