简体   繁体   English

实体框架代码首先用于具有属性集合的对象

[英]Entity Framework code first for object with collection of properties

I'm using C# and .Net core with MySql and Entity Framework.我在 MySql 和实体框架中使用 C# 和 .Net 核心。

I have an object with a collection of properties.我有一个带有属性集合的对象。 Like this:像这样:

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

In this case in the DB I should have tables Products, Properties(where property is described, like name and some additional info), and link table ProductProperties, storing product Id, proeprty Id and Value.在这种情况下,在 DB 中,我应该有表 Products、Properties(其中描述了属性,如名称和一些附加信息)和链接表 ProductProperties,存储产品 Id、property Id 和值。

But I can't figure out how to do this with Code first approach.但我无法弄清楚如何使用代码优先方法来做到这一点。

How could I implement it with code first?我怎么能先用代码实现它? Is it a good way to create one more entity PropertyValue and store it under Product?创建另一个实体 PropertyValue 并将其存储在 Product 下是一种好方法吗?

Something like this should give you a 1-to-many relationship, although you need to give Value a type, like string to store it in the database, often for dynamic solutions like this you would then maybe add a type to specify the type to deserialize into, but since you then deserialize anyway you could also just add things as json or something else in the db.像这样的东西应该给你一个 1 对多的关系,虽然你需要给 Value 一个类型,比如字符串来将它存储在数据库中,通常对于这样的动态解决方案,你可能会添加一个类型来指定类型反序列化,但既然你反序列化了,你也可以在数据库中添加 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;}
}

Unless you are making a very dynamic system, it doesn't seem right to have the properties as a table, depends a lot of what you are making, and maybe key-value db might be a better tool for the job if thats what your main problem is, as with most complicated things, it depends.除非您正在制作一个非常动态的系统,否则将属性作为表似乎是不正确的,这在很大程度上取决于您正在制作的内容,如果那是您的工作,那么键值数据库可能是一个更好的工具主要问题是,与大多数复杂的事情一样,这取决于。

This example is a convention based approach, which is why properties like ProductId have to be called exactly that.此示例是基于约定的方法,这就是必须准确调用 ProductId 等属性的原因。 You can look at EntityTypeConfigurations if you want more control of names and relationships and such, or use data annotations to achieve the same job.如果您想要更多地控制名称和关系等,或者使用数据注释来实现相同的工作,您可以查看 EntityTypeConfigurations。

Ok so create a table like this:好的,创建一个这样的表:

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
}

If you are using EntityFramework Core, then you have to add this to your databse context as well:如果您使用的是 EntityFramework Core,那么您还必须将其添加到您的数据库上下文中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<ProdcutProprties>().HasKey(x => new { x.ProductId , x.PropertyId });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM