复杂类型:一个模型中的多个实例?

时间:2013-08-19 10:33:15

标签: c# entity-framework ef-code-first

使用Fluent api模型构建器,是否可以在同一模型中拥有多个复杂类型的实例?

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);
    }
}

3 个答案:

答案 0 :(得分:1)

使用EF 6时,当您使用相同复杂类型的多个实例时,Code First将使用您的属性名称为列名添加前缀,例如

PersonalAddress_Street
BusinessAddress_Street
...

等等。您只需要映射旧版本的EF。映射选项还允许您用更好的东西替换这些自动生成的名称。

此外,我不认为这是基于您的模型的正确配置代码(错误可能?):

this.Property(t => t.PersonalAddress.Address.Street)
// should be
this.Property(t => t.PersonalAddress.Street)

答案 1 :(得分:1)

(问题中的自动回答部分转换为真实答案)

如果在同一CF模型中使用了多个复杂类型类实例,则这些类的配置将在CF模型级别进行设置,如下所示:

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");
    }
}

答案 2 :(得分:0)

使用数据注释[ComplexType]可能更简单,并且EF将自动设置名称。以您的示例为例:

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在联系人实体中创建所有字段,并将其命名为: PersonalAddress_Street,BusinessAddress_Street,PersonalAddress_City,BusinessAddress_City等...