简体   繁体   English

使用代码优先和自定义EntityTypeConfiguration实现将复杂类型的属性映射到EF 6中的DB列

[英]Mapping Properties of type of Complex Type to DB Columns in EF 6 using code first and custom EntityTypeConfiguration implementation

This is my first activity with great stack overflow, but absolutely not my first time to visit the website. 这是我的第一次活动,堆栈溢出很大,但是绝对不是我第一次访问该网站。

My Question: 我的问题:
In our project, we are extending the EntityTypeConfiguration to take control over the mapping of entities to the DB in code first fluent API. 在我们的项目中,我们正在扩展EntityTypeConfiguration以控制通过代码优先流利的API将实体映射到DB的控制。
What I am confused with is how it is possible to include the configuration of complex types in the ctor of derived configuration, ie The configuration of primitive properties is possible through "Property" method of base EntityTypeConfiguration, but when calling this method on properties of type of Complex Types, the project builds will stop at called method. 我感到困惑的是如何在派生配置的ctor中包括复杂类型的配置,即可以通过基本EntityTypeConfiguration的“ Property”方法配置基本属性,但是在对类型的属性调用此方法时对于复杂类型,项目构建将在被调用的方法处停止。

Actually, the DB will be generated by entity table containing multiple columns, representing the fields of the embedded Complex Type using "PROPERTYNAME_INNERFIELDNAME" naming convention, even though if the complex type be of class type. 实际上,数据库将由包含多列的实体表生成,这些列使用"PROPERTYNAME_INNERFIELDNAME"命名约定代表嵌入式复杂类型的字段,即使复杂类型是类类型也是如此。

So, I do not know how to overcome with the issue if the complex type be of class type. 因此,如果复杂类型为类类型,我不知道该如何克服。

Thank you for your kind consideration. 感谢您的友好考虑。

My Code: 我的代码:

class Entity
{
    long Id;
    Address Addr;
}

class Address
{
    string City;
    string ZipCode;
}

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

Firstly, thanks to dear Bardware. 首先,感谢亲爱的Bardware。

My problem has been solved by using following configuration: 我的问题已通过使用以下配置解决:

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

Actually, I could not use "ComplexTypeConfiguration" instead of EntityTypeConfiguration because I would loose my chance to use HasKey, HasRequired etc. 实际上,我不能使用“ ComplexTypeConfiguration”代替EntityTypeConfiguration,因为我会失去使用HasKey,HasRequired等的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM