Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用实体框架处理自定义字段_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Ef Code First - Fatal编程技术网

C# 使用实体框架处理自定义字段

C# 使用实体框架处理自定义字段,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,ef-code-first,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Ef Code First,目前,我有一个项目需要处理带有用户可以指定的自定义字段的产品 有人知道使用模型和代码优先注释实现此功能的最佳方法吗 我想在数据库方面,我需要一个新表来处理将链接到productID的自定义数据。您可以为用户可以添加的自定义属性指定一个单独的表。此自定义字段将引用您的主模型。基本上,您将拥有1到M关系 public class MyModel { public int MyModelID { get; set; } public string FixedPr

目前,我有一个项目需要处理带有用户可以指定的自定义字段的产品

有人知道使用模型和代码优先注释实现此功能的最佳方法吗


我想在数据库方面,我需要一个新表来处理将链接到productID的自定义数据。

您可以为用户可以添加的自定义属性指定一个单独的表。此自定义字段将引用您的主模型。基本上,您将拥有
1到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; }
}
公共类MyModel
{
公共int MyModelID{get;set;}
公共字符串FixedProperty1{get;set;}
公共字符串FixedProperty2{get;set;}
//这是所有自定义属性的导航属性
公共虚拟ICollection CustomProperties{get;set;}
}
公共类自定义属性
{
public int CustomPropertyID{get;set;}
//这是自定义字段的名称
公共字符串PropertyName{get;set;}
//这就是它的价值所在
公共字符串PropertyValue{get;set;}
//主表的FK引用和导航属性
公共int MyModelID{get;set;}
公共虚拟MyModel MyModel{get;set;}
}

您可以为用户可以添加的自定义属性指定单独的表。此自定义字段将引用您的主模型。基本上,您将拥有
1到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; }
}
公共类MyModel
{
public int MyModelID{get;set;}
公共字符串FixedProperty1{get;set;}
公共字符串FixedProperty2{get;set;}
//这是所有自定义属性的导航属性
公共虚拟ICollection CustomProperties{get;set;}
}
公共类自定义属性
{
public int CustomPropertyID{get;set;}
//这是自定义字段的名称
公共字符串PropertyName{get;set;}
//这就是它的价值所在
公共字符串PropertyValue{get;set;}
//主表的FK引用和导航属性
公共int MyModelID{get;set;}
公共虚拟MyModel MyModel{get;set;}
}

将其指定为
虚拟
功能的目的是什么?虚拟在EF上下文中有不同的含义。它提供延迟加载。基本上,当您从db获取MyModel时,除非您明确请求,否则它不会带来自定义属性。如果您认为大多数时候都需要模型本身的自定义属性,那么可以安全地删除virtual关键字。更多信息请点击这里。哦,好吧,我从我的条令经验中了解到了延迟加载,我没有意识到使用
virtual
是EF的处理方式。非常感谢您提供的信息!将其指定为
virtual
函数的目的是什么?virtual在EF上下文中有不同的含义。它提供延迟加载。基本上,当您从db获取MyModel时,除非您明确请求,否则它不会带来自定义属性。如果您认为大多数时候都需要模型本身的自定义属性,那么可以安全地删除virtual关键字。更多信息请点击这里。哦,好吧,我从我的条令经验中了解到了延迟加载,我没有意识到使用
virtual
是EF的处理方式。非常感谢您提供的信息!