重构EF6实体而不是使用多个属性使用复杂类型

时间:2014-03-07 19:56:58

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

我有一个班级客户,其中包含地址属性,电话属性和传真属性,但我想将地址,电话属性转换为复杂类型。属性是否已作为列存在于数据库中。

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

}

与数据库中已存在的内容没有冲突?

1 个答案:

答案 0 :(得分:0)

您的地址类应使用ComplexType属性进行注释,并且您需要显式映射列名称:

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

    // Other properties
}