1

それぞれがカスタム フィールドを格納できる 3 つの異なるモデル/テーブルがあります。私の 3 つのモデルはCustomer ProductStockです。

CustomData3 つのモデルすべてのカスタム データを処理できる単一のテーブルにマップされたモデルを作成する方法はありますか? 現在、モデルごとに個別のテーブル/モデルがあります。

顧客モデル:

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

1 に答える 1

0

ええ、できますが、現在のアプローチの方が優れていると思います。とにかく、クラスの 1 つを拡張して、他のプロパティ (および場合によってはメソッド) を含める必要があります。この場合は、他のクラスのプロパティです。

これを行うには、これらのエンティティ クラスの 1 つと同じ名前と同じアセンブリ名を持つ部分クラス some where を作成します。

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...
    }
}
于 2013-07-25T05:20:34.397 に答える