Refactor EF6 entity to instead of use multiple properties use a complex type

心已入冬 提交于 2019-12-25 16:42:57

问题


I have a class customer that contains address properties, phone properties and fax properties, but I want to take off the address, phone properties to complex types. Does properties are already in the database as columns.

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

how to refactor this into:

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

}

without conflicting with what already exist in the database?


回答1:


Your address class should be annotated with the ComplexType attribute and you'd need to explicitly map the column names:

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

    // Other properties
}


来源:https://stackoverflow.com/questions/22259531/refactor-ef6-entity-to-instead-of-use-multiple-properties-use-a-complex-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!