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的处理方式。非常感谢您提供的信息!