本文介绍了重构EF6实体而不是使用多个属性使用复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类客户包含地址属性,电话属性和传真属性,但是我想将地址,电话属性取消为复杂类型。属性已作为列在数据库中。 [Table(tblCustomer)]
public partial class Customer:Entity
{
[Key]
public int CustomerID {get;组;
[StringLength(10)]
public string CustomerCode {get;组;
[StringLength(60)]
public string AddressLine1 {get;组;
[StringLength(70)]
public string AddressLine2 {get;组; }
[StringLength(35)]
public string City {get;组;
[StringLength(2)]
public string State {get;组; }
[StringLength(10)]
public string ZipCode {get;组; }
[StringLength(15)]
public string PhoneNo {get;组;
[StringLength(3)]
public string PCountryCode {get;组;
[StringLength(3)]
public string PAreaCode {get;组; }
[StringLength(7)]
public string PPhoneNo {get;组;
[StringLength(3)]
public string FCountryCode {get;组; }
[StringLength(3)]
public string FAreaCode {get;组;
[StringLength(7)]
public string FaxNumber {get;组;
[StringLength(3)]
public string CountryCode {get;组;
}
如何将其重构为:
[Table(tblCustomer)]
public partial class Customer:Entity
{
[Key]
public int CustomerID {get;组;
[StringLength(10)]
public string CustomerCode {get;组; }
public Address Address {get;组; }
public Phone Phone {get;组; }
public Phone Fax {get;组; }
}
不与数据库中已存在的冲突? / p>
解决方案
您的地址类应该使用 ComplexType
属性注释, d需要明确地列出列名:
[ComplexType]
public class Address
{
[Column(street)]
public string Street {get; set;}
//其他属性
}
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?
解决方案
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
}
这篇关于重构EF6实体而不是使用多个属性使用复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!