首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个模型的自定义数据模型

多个模型的自定义数据模型
EN

Stack Overflow用户
提问于 2013-06-11 16:06:51
回答 1查看 139关注 0票数 1

我有3个不同的模型/表,每个都可以存储自定义字段。我的3个型号是Customer ProductStock

有没有办法创建一个映射到单个表的CustomData模型来处理所有3个模型的自定义数据?目前,我为每个模型都有一个单独的表/模型。

客户模型:

代码语言:javascript
复制
[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; }
}

产品型号:

代码语言:javascript
复制
[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; } 
}

自定义产品型号属性:

代码语言:javascript
复制
[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; }
}
EN

回答 1

Stack Overflow用户

发布于 2013-07-25 13:20:34

是的,你可以,但是我认为目前的方法更好。无论如何,您应该扩展一个类以包含其他属性(可能还有方法)-在本例中,包含其他类的属性。

为此,在某些地方创建一个分部类,与其中一个实体类具有相同的名称和相同的程序集名称。

假设EF为您生成以下实体类:

代码语言:javascript
复制
namespace myPrj
{
    public partial class Product
    {
        // auto generated class
    }
}

现在,创建一个分部类,如下所示:

代码语言:javascript
复制
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...
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17039256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档