gpt4 book ai didi

entity-framework - 首先编码实体配置

转载 作者:行者123 更新时间:2023-12-02 20:33:56 24 4
gpt4 key购买 nike

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

示例:我在名为 Framework 的解决方案中名为 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 类,该类从 Framework 解决方案中的 Data.Access 扩展 Context。它的 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 Context 类的 Dispose 函数中使用 Database.ExecuteSqlCommand。不优雅,但有效。任何想法/建议将不胜感激。

谢谢

最佳答案

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

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 方法。

关于entity-framework - 首先编码实体配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13253497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com