Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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#_Entity Framework_.net Core_Ef Code First_Code First - Fatal编程技术网

C# 具有属性集合的对象的实体框架代码

C# 具有属性集合的对象的实体框架代码,c#,entity-framework,.net-core,ef-code-first,code-first,C#,Entity Framework,.net Core,Ef Code First,Code First,我正在使用C#和.Net内核以及MySql和实体框架 我有一个具有属性集合的对象。像这样: public class Product { public int Id {get; set;} public string Name{get; set;} public IEnumarable<Property> Properties{get; set;} } public class Property { public int Id {get; set;} publi

我正在使用C#和.Net内核以及MySql和实体框架

我有一个具有属性集合的对象。像这样:

public class Product
{
  public int Id {get; set;}
  public string Name{get; set;}
  public IEnumarable<Property> Properties{get; set;}
}

public class Property
{
  public int Id {get; set;}
  public string Name{get; set;}
  public object Value{get; set;}
}
公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共IEnumarable属性{get;set;}
}
公共类财产
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共对象值{get;set;}
}
在这种情况下,在DB中,我应该有表Products、property(其中描述了属性,如名称和一些附加信息)和链接表PRODUCTPROPERTYS,存储产品Id、proeprty Id和值

但我不知道如何使用代码优先的方法来实现这一点

如何首先用代码实现它?
创建一个或多个实体PropertyValue并将其存储在Product下,这是一种好方法吗?

类似的方法应该会为您提供一对多的关系,尽管您需要为值指定一个类型,如字符串以将其存储在数据库中,通常对于这样的动态解决方案,您可能会添加一个类型来指定要反序列化的类型,但是,既然您随后反序列化了,那么您也可以在数据库中添加json或其他内容

public class Product
{
  public int Id {get; set;}
  public string Name{get; set;}
  public ICollection<Property> Properties{get; set;}
}

public class Property
{
  public int Id {get; set;}
  public string Name {get; set;}
  public string Value {get; set;}
  public int ProductId {get; set;}
}
公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection属性{get;set;}
}
公共类财产
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
public int ProductId{get;set;}
}
除非您正在创建一个非常动态的系统,否则将属性作为一个表似乎是不正确的,这在很大程度上取决于您正在创建的内容,而且如果这是您的主要问题所在,那么key-value-db可能是一个更好的工具,这取决于最复杂的事情


这个例子是基于约定的方法,这就是为什么像ProductId这样的属性必须被精确地调用的原因。如果希望更多地控制名称和关系等,可以查看EntityTypeConfiguration,也可以使用数据注释来完成相同的任务。

好,创建如下表:

public class ProductProprties
{
  public int ProductId {get; set;}
  public Product Product {get;set;}
  public int PropertyId {get; set;}
  public Property Property {get;set;}      
  //other props
}
如果您使用的是EntityFramework Core,则还必须将其添加到数据库上下文中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<ProdcutProprties>().HasKey(x => new { x.ProductId , x.PropertyId });
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().HasKey(x=>new{x.ProductId,x.PropertyId});
}

您到底有什么问题?您无法创建表或关系或获取查询?请看EAV。这是您正在寻找的(反)模式的名称。是否要具有额外属性的多对多表?@Vince是的,没错。