簡體   English   中英

使用實體框架處理自定義字段

[英]Handling Custom Fields using the Entity Framework

目前,我有一個項目需要處理具有用戶可以指定的自定義字段的產品。

有誰知道使用模型和代碼優先注釋實現這個的最佳方法是什么?

我想在數據庫方面,我需要一個新表來處理將鏈接到productID的自定義數據。

您可以為用戶可以添加的自定義屬性指定單獨的表。 此自定義字段將引用您的主模型。 基本上,你將擁有1 to M關系

public class MyModel
{
    public int MyModelID            { get; set; }

    public string FixedProperty1    { get; set; }
    public string FixedProperty2    { get; set; }

    // This is a navigation property for all your custom properties
    public virtual ICollection<CustomProperty> CustomProperties  { get; set; }
}

public class CustomProperty
{
    public int CustomPropertyID      { get; set; }

    // This is the name of custom field
    public string PropertyName       { get; set; }
    // And this is its value
    public string PropertyValue      { get; set; }

    // FK reference and navigation property to your main table
    public int MyModelID             { get; set; }
    public virtual MyModel MyModel   { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM