重构EF6实体的,而不是使用多个属性中使用复杂类型(Refactor EF6 entity to i

2019-11-03 01:38发布

我有一个一流的客户包含地址性质,手机性能和传真性能,但我想起飞的地址,电话属性,复杂类型。 性能是否已经在数据库中列。

   [Table("tblCustomer")]
public partial class Customer : Entity 
{
    [Key]
    public int CustomerID { get; set; }

    [StringLength(10)]
    public string CustomerCode { get; set; }

    [StringLength(60)]
    public string AddressLine1 { get; set; }

    [StringLength(70)]
    public string AddressLine2 { get; set; }

    [StringLength(35)]
    public string City { get; set; }

    [StringLength(2)]
    public string State { get; set; }

    [StringLength(10)]
    public string ZipCode { get; set; }

    [StringLength(15)]
    public string PhoneNo { get; set; }

    [StringLength(3)]
    public string PCountryCode { get; set; }

    [StringLength(3)]
    public string PAreaCode { get; set; }

    [StringLength(7)]
    public string PPhoneNo { get; set; }

    [StringLength(3)]
    public string FCountryCode { get; set; }

    [StringLength(3)]
    public string FAreaCode { get; set; }

    [StringLength(7)]
    public string FaxNumber { get; set; }

    [StringLength(3)]
    public string CountryCode { get; set; }
}

如何重构到这一点:

[Table("tblCustomer")]
public partial class Customer : Entity 
{
    [Key]
    public int CustomerID { get; set; }

    [StringLength(10)]
    public string CustomerCode { get; set; }

    public Address Address { get; set; }

    public Phone Phone { get; set; }

    public Phone Fax { get; set; }

}

没有什么已经在数据库中存在相互矛盾的?

Answer 1:

您的地址类别应与被标注ComplexType属性,你需要明确地映射列名:

[ComplexType]
public class Address
{
    [Column("street")]
    public string Street {get; set;}

    // Other properties
}


文章来源: Refactor EF6 entity to instead of use multiple properties use a complex type