我有3个不同的模型/表,每个都可以存储自定义字段。我的3个型号是Customer Product和Stock。
有没有办法创建一个映射到单个表的CustomData模型来处理所有3个模型的自定义数据?目前,我为每个模型都有一个单独的表/模型。
客户模型:
[Table("Customer")]
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomerID { get; set; }
public string Prefix { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public string Address { get; set; }
public string CountryID { get; set; }
[ForeignKey("CountryID")]
public virtual Country Country { get; set; }
public string City { get; set; }
[Display(Name = "Postcode")]
public string PostCode { get; set; }
//[RegularExpression(@"\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b")]
public string Email { get; set; }
public string Remarks { get; set; }
[Display(Name = "Telephone")]
public string PhoneNumber { get; set; }
public ICollection<CustomCustomerProperty> CustomProperties { get; set; }
}产品型号:
[Table("Product")]
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public int ProductTypeID { get; set; }
public string ProductName { get; set; }
[ForeignKey("ProductTypeID")]
public virtual ProductType ProductType { get; set; }
public ICollection<CustomProductData> CustomProperties { get; set; }
}自定义产品型号属性:
[Table("CustomProductData")]
public class CustomProductData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomPropertyID { get; set; }
public int CustomProductPropertyID { get; set; }
[ForeignKey("CustomProductPropertyID")]
public virtual CustomProductProperty CustomProductPropertyType { get; set; }
public int ProductID { get; set; }
[ForeignKey("ProductID")]
public virtual Product Product { get; set; }
public string PropertyValue { get; set; }
}发布于 2013-07-25 13:20:34
是的,你可以,但是我认为目前的方法更好。无论如何,您应该扩展一个类以包含其他属性(可能还有方法)-在本例中,包含其他类的属性。
为此,在某些地方创建一个分部类,与其中一个实体类具有相同的名称和相同的程序集名称。
假设EF为您生成以下实体类:
namespace myPrj
{
public partial class Product
{
// auto generated class
}
}现在,创建一个分部类,如下所示:
namespace myPrj
{
public partial class Product
{
// your partial class.
// Add properties of other entity classes here
// and you'll have all of them within the Product entity class...
}
}https://stackoverflow.com/questions/17039256
复制相似问题