Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex type: multiple instances in one model?

Is there a way to have multiple instances of complex type inside the same model using Fluent api model builder?

public class Contact
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }

     public Address PersonalAddress { get; set; }
     public Address BusinessAddress { get; set; }
}

public class Address
{
     public string Street{ get; set; }
     public string City{ get; set; }
     public string PostalCode{ get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

     modelBuilder.Configurations.Add(new ContactConfiguration());
     modelBuilder.Configurations.Add(new AddressConfiguration());
}

==================================================================================

public class AddressConfiguration : ComplexTypeConfiguration<Address>
{
    public AddressConfiguration()
    {

        //props
        this.Property(t => t.Street)
            .IsOptional()
            .HasColumnName("AddressStreet")
            .HasMaxLength(1024);

        this.Property(t => t.PostalCode)
            .IsOptional()
            .HasColumnName("AddressPostalCode")
            .HasMaxLength(64);

        this.Property(t => t.City)
            .IsOptional()
            .HasColumnName("AddressCity")
            .HasMaxLength(512);
    }
}
like image 558
Davor Zubak Avatar asked Aug 19 '13 10:08

Davor Zubak


3 Answers

With EF 6, Code First will prefix the column names with your Property name when you use multiple instances of same complex type, like

PersonalAddress_Street
BusinessAddress_Street
...

and so on. You only need the mappings older versions of EF. The mapping option also allows you to replace these autogenerated names with something nicer.

Also, I don't think this is the correct configuration code based on your models (typo maybe?):

this.Property(t => t.PersonalAddress.Address.Street)
// should be
this.Property(t => t.PersonalAddress.Street)
like image 90
Mrchief Avatar answered Oct 18 '22 15:10

Mrchief


It's possible and simpler to use data annotation [ComplexType] and EF will set the names automatically. Using your example:

public class Contact
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }

     public Address PersonalAddress { get; set; }
     public Address BusinessAddress { get; set; }
}

[ComplexType]
public class Address{
    public string Street{ get; set; }
    public string City{ get; set; }
    public string PostalCode{ get; set; }
}

EF creates all fields in Contact Entity and name it like: PersonalAddress_Street, BusinessAddress_Street, PersonalAddress_City, BusinessAddress_City, etc...

like image 1
Guilherme Muniz Avatar answered Oct 18 '22 14:10

Guilherme Muniz


(Auto-answered part in the question converted to a real answer)

If multiple complex type class instances are used in same CF Model, configuration of these classes is set at CF Model level like this:

public class ContactConfiguration : EntityTypeConfiguration<Contact>
{
    public ContactConfiguration()
    {

        //props for PersonalAddress instance of Address complex type class
        this.Property(t => t.PersonalAddress.Address.Street)
            .HasColumnName("PersonalAddressStreet");

        this.Property(t => t.PersonalAddress.Address.PostalCode)
            .HasColumnName("PersonalAddressPostalCode");

        this.Property(t => t.PersonalAddress.Address.City)
            .HasColumnName("PersonalAddressCity");


        //props for BusinessAddress instance of Address complex type class
        this.Property(t => t.BusinessAddress.Address.Street)
            .HasColumnName("BusinessAddressStreet");

        this.Property(t => t.BusinessAddress.Address.PostalCode)
            .HasColumnName("BusinessAddressPostalCode");

        this.Property(t => t.BusinessAddress.Address.City)
            .HasColumnName("BusinessAddressCity");
    }
}
like image 1
Gert Arnold Avatar answered Oct 18 '22 13:10

Gert Arnold