简体   繁体   English

如何在数据库中存储两个相同的实体? 实体框架,C#

[英]How to store two same entities in a database? Entity framework, c#

I have two entities with exactly the same properties: 我有两个属性完全相同的实体:

public class Oil
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public int Price { get; set; }
    public int Ammount { get; set; }
}

public class Filter
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public int Price { get; set; }
    public int Ammount { get; set; }
}

Questions: 问题:

1) Can I somehow store them in one table? 1)我可以以某种方式将它们存储在一张桌子中吗? If so, than how? 如果是这样,那又如何?

2) Or should I implement inheritance? 2)还是应该实现继承? And what type then? 那是什么类型的呢?

Edits: 编辑:

In my case these two entities are just the same, they will not have any different properties in the future. 就我而言,这两个实体是相同的,将来它们将没有任何不同的属性。

I implemented Table-per-Hierarchy approach, but there is another issue 我实现了“逐层表”方法,但是还有另一个问题
(I have another type that has collections of oils and filters): (我还有另一种类型的油和滤清器):

public class Warehouse
{
    public Guid Id { get; set; } 
    public ICollection<Filter> Filters { get; set; }
    public ICollection<Oil> Oils { get; set; }
}

So, when I create database, I get Warehouse_Id and Warehouse_Id1 fields in it. 因此,当我创建数据库时,会在其中获取Warehouse_IdWarehouse_Id1字段。 I don't want the Oil and Filter classes to have Warehouse property in them, how can I get just one field for Warehouse id in the db table? 我不希望Oil和Filter类中具有Warehouse属性,如何在db表中仅获得Warehouse ID的一个字段?

If I include WarehouseId as a property in OilFilterBase class I will get 3 warehouse_id in the database table. 如果我将WarehouseId作为OilFilterBase类中的属性包括在内,我将在数据库表中获得3 Warehouse_id。

ps I also have DbSet<Oil> and DbSet<Filter> in my Context and don't have DbSet<OilFilterBase> . ps我在Context也有DbSet<Oil>DbSet<Filter> ,没有DbSet<OilFilterBase>

It's hard to say what's best without knowing more about your requirements. 在不了解您的要求的情况下很难说出最好的方法。 What makes these two entities different? 是什么使这两个实体不同? If they perform different functions and just happen to have the same properties, then it would probably be a good idea to store them in separate tables; 如果它们执行不同的功能并且恰好具有相同的属性,那么将它们存储在单独的表中可能是一个好主意; that makes the most sense conceptually, and it would make things much easier if, say, you decided you wanted to add additional properties to one of them in the future. 从概念上讲,这是最有意义的,并且,例如,如果您决定将来想向其中的一个添加其他属性,它将使事情变得容易得多。

On the other hand, if they're really the same at every level, it's also worth asking if you really need two different entity types to store them. 另一方面,如果它们在每个级别上确实相同,那么还值得询问您是否真的需要两种不同的实体类型来存储它们。

For the middle ground where the two classes serve related purposes but also differ in some ways, then yes, some form of inheritance might be a good approach -- either having one entity type derive from the other, or creating a new common base type and having both entities derive from that. 对于这两个类服务于相关目的但在某些方面有所不同的中间立场,那么是的,某种形式的继承可能是一种好方法-使一个实体类型从另一实体类型派生,或者创建一个新的通用基本类型,然后让两个实体都从中得出。

If you decide this is the best approach, then it looks like a good candidate for Table-per-Hierarchy mapping. 如果您确定这是最好的方法,那么它似乎是逐层映射的理想选择。 You could restructure your code something like this: 您可以这样来重组代码:

public abstract class OilFilterBase
{
  public Guid Id { get; set; }
  public string Title { get; set; }
  public int Price { get; set; }
  public int Amount { get; set; }
}

public class Oil : OilFilterBase
{
}

public class Filter : OilFilterBase
{
}

...and then the Entity Framework will, by default, create a single table with an automatically-generated discriminator column, and store all instances of both entity types in that table. ...然后,默认情况下,实体框架将使用自动生成的区分符列创建一个表,并将两种实体类型的所有实例存储在该表中。

If you decide that either of those entity types should have additional fields, then you could look at some of the other inheritance options, like Table-per-Type , that create separate but related tables for each entity type. 如果您决定这些实体类型中的任何一个都应具有其他字段,则可以查看其他一些继承选项,例如Table-per-Type ,它们为每种实体类型创建单独但相关的表。

The first thing to do is decide how these classes fit together conceptually, and then figure out the best way to implement that in EF terms. 首先要做的是确定这些类在概念上如何组合在一起,然后找出用EF术语实现这些类的最佳方法。 If you can give more information about what these entities are and how they work, it'll be easier for people here to give good advice. 如果您可以提供有关这些实体是什么以及它们如何工作的更多信息,那么这里的人们将更容易提供良好的建议。

Response to Edits: 对编辑的回应:

I think what's happening with the extra columns ( Warehouse_Id and Warehouse_Id1 ) is this: 我认为多余的列( Warehouse_IdWarehouse_Id1 )是这样的:

Because you're setting up the relationships for Oil and Filter separately, it's not comfortable assuming you want to use the base class's WarehouseId property as the foreign key -- what if you only wanted to set up that relationship for Oil and not Filter ? 因为您要分别设置OilFilter的关系,所以假设您要使用基类的WarehouseId属性作为外键是不舒服的-如果只想为Oil而不是Filter设置该关系呢? It shouldn't be writing to the base class column in that case. 在这种情况下,它不应该写入基类列。 So, it decides to create new properties instead. 因此,它决定创建新属性。

Fortunately, you can use the [ForeignKey()] attribute (or the fluent API) to tell it what you really want, like this: 幸运的是,您可以使用[ForeignKey()]属性(或流畅的API)告诉它您真正想要的是什么,如下所示:

using System.ComponentModel.DataAnnotations.Schema;

public abstract class OilFilterBase
{
  public Guid Id { get; set; }
  public string Title { get; set; }
  public int Price { get; set; }
  public int Amount { get; set; }
  public Guid WarehouseId { get; set; }
}

public class Oil : OilFilterBase
{
}

public class Filter : OilFilterBase
{
}

public class Warehouse
{
  public Guid Id { get; set; }

  [ForeignKey("WarehouseId")]
  public virtual ICollection<Filter> Filters { get; set; }

  [ForeignKey("WarehouseId")]
  public virtual ICollection<Oil> Oils { get; set; }
}

Also, I think you'll need to include a DbSet<OilFilterBase> (in addition to DbSet<Oil> and DbSet<Filter> ) in your context in order to get Table-per-Hierarchy inheritance to work -- try it and see. 另外,我认为您需要在上下文中包括DbSet<OilFilterBase> (除了DbSet<Oil>DbSet<Filter> ),以使“ DbSet<Filter>表继承”起作用-尝试一下,看看。

Good luck! 祝好运!

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

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