我有 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; }
}