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

StackOverflow https://stackoverflow.com/questions/21553789

Question

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.
What I am confused with is how it is possible to include the configuration of complex types in the ctor of derived configuration, i.e. 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.

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.

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
    }
}
Was it helpful?

Solution

Firstly, thanks to dear 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top