【问题标题】:Mapping Properties of type of Complex Type to DB Columns in EF 6 using code first and custom EntityTypeConfiguration implementation使用代码优先和自定义 EntityTypeConfiguration 实现将复杂类型类型的属性映射到 EF 6 中的 DB 列
【发布时间】:2014-02-04 13:25:35
【问题描述】:

这是我第一次出现大量堆栈溢出的活动,但绝对不是我第一次访问该网站。

我的问题:
在我们的项目中,我们正在扩展 EntityTypeConfiguration 以在代码优先的 fluent API 中控制实体到数据库的映射。
我感到困惑的是如何在派生配置的ctor中包含复杂类型的配置,即可以通过基本EntityTypeConfiguration的“Property”方法配置原始属性,但是在对类型的属性调用此方法时复杂类型,项目构建将停止在调用的方法。

实际上,数据库将由包含多个列的实体表生成,使用"PROPERTYNAME_INNERFIELDNAME"命名约定表示嵌入的复杂类型的字段,即使复杂类型是类类型。

所以,如果复杂类型是类类型,我不知道如何克服这个问题。

感谢您的关心。

我的代码:

class Entity
{
    long Id;
    Address Addr;
}

class Address
{
    string City;
    string ZipCode;
}

class EntityConfig : EntityTypeConfiguration<Entity>
{
    EntityConfig()
    {
        Property(p=>p.Addr);  //Build error
    }
}

【问题讨论】:

  • 您必须将每个成员映射到一个列;使用ComplexTypeConfiguration 而不是EntityTypeConfiguration

标签: c# entity-framework ef-code-first fluent-interface complextype


【解决方案1】:

首先,感谢亲爱的 Bardware。

使用以下配置解决了我的问题:

Property(p=>p.Addr.City);
Property(p=>p.Addr.ZipCode);

实际上,我不能使用“ComplexTypeConfiguration”代替 EntityTypeConfiguration,因为我会失去使用 HasKey、HasRequired 等的机会。

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多