简体   繁体   English

使用SQL Server和实体框架为相关实体建模

[英]Modeling related entities with SQL Server and entity framework

I've got a table in my database storing items: 我的数据库中有一个表,用于存储项目:

Items
-------
ItemID
Name
...
Etc

and a separate table storing the PK of two different items from the first table. 还有一个单独的表,用于存储与第一个表不同的两个项目的PK。 I want to be able to list the one item, and then any number of related items. 我希望能够列出一个项目,然后列出任何数量的相关项目。 I've tried searching for examples but haven't found much surprisingly... 我尝试搜索示例,但没有发现太多令人惊讶的地方...

RelatedItems
------------
ItemID
RelatedItemID

If I have four products, whose IDs are 1, 2, 3 and 4... and 1 is related to 2 and 3 I might have data that looks like this: 如果我有四个产品,其ID为1、2、3和4 ...,并且1与2和3有关,则我可能具有如下数据:

ItemID   RelatedItemID
1        2  
1        3  
4        1  

I am then modeling them in the Entity Framework Designer, and the designer automatically adds an association from the Items table to itself (many to many). 然后,我在实体框架设计器中对其进行建模,然后设计器自动将Items表中的关联添加到自身(很多到很多)中。 The designer also adds two navigation properties, if I use the first property on Item #1 I get all items where Item #1 is in the first column, and if I use the second property I get all the items where Item #1 is in the second column. 设计器还添加了两个导航属性,如果我在Item#1上使用第一个属性,则获得第一列中项目#1所在的所有项目,如果我使用第二个属性,则在Item#1中获得所有项目。第二列。

I however just want to have one navigation property where I can say Items.RelatedItems and it returns all the items that the above two properties would when combined. 但是,我只想拥有一个导航属性,在其中我可以说Items.RelatedItems,它返回上述两个属性结合在一起时的所有项目。 I know I can join the two results after the fact but I can't help to think I'm doing something wrong and there is a better way. 我知道事后我可以将这两个结果加在一起,但我不禁会认为自己做错了什么,还有更好的方法。

Hopefully this is all clear enough. 希望这一切已经足够清楚了。

It sounds like SQL schemas just aren't very good at modeling the concept you're looking for. 听起来SQL模式只是不太擅长建模您要寻找的概念。 The schema you've chosen would work well if you want to establish a directional relationship (item A is related to item B, but item B may or may not be related to item A). 如果您要建立方向关系(项目A与项目B相关,但项目B可能与项目A相关,也可能不相关),那么您选择的模式会很好用。 If you were looking for a grouping-style relationship (Items A and B are in the same group), I can think of a different approach you'd use. 如果您正在寻找一种分组样式的关系(项目A和B在同一个组中),我可以考虑使用一种不同的方法。 But I can't think of a good way to model an inherently bi-directional relationship using a traditional relational database. 但是我想不出一种使用传统的关系数据库为固有的双向关系建模的好方法。

Some workarounds might be to use a View that joins the two results, or to use triggers to make sure that every mapping from A to B has a corresponding mapping from B to A, so that both of the properties always return the same objects. 一些解决方法可能是使用将两个结果连接在一起的View,或者使用触发器来确保从A到B的每个映射都具有从B到A的对应映射,以便两个属性始终返回相同的对象。

If you have an instance of an Item, call it item, then the following will give you the related items... 如果您有一个Item的实例,将其称为item,那么以下内容将为您提供相关的...

item.RelatedItems.Select(ri => ri.Item);

Your RelatedItems property on item (ie the first navigation property you mentioned) will be a collection of RelatedItem objects, each of which has two navigation properties of its own, one of which will be named Item and will be a link to the related item. 您在项目上的RelatedItems属性(即您提到的第一个导航属性)将是RelatedItem对象的集合,每个对象都有其自己的两个导航属性,其中一个将被命名为Item,并将成为相关项目的链接。

Note that this is air code, as I'm not in front of anything I can test this on right now, but I think this will do what you want. 请注意,这是空代码,因为我现在什么都可以测试,但是我认为这可以满足您的要求。

If you want to make it simpler, you can write an extension method to wrap up the Select(), something like this... 如果您想简化它,可以编写一个扩展方法来包装Select(),就像这样...

public static IEnumerable<Item> RelItems(this Item item) {
  return item.RelatedItems.Select(ri => ri.Item);
}

Then you could just do... 那你就可以做...

item.RelItems();

Note that I couldn't name the extension method RelatedItems, as that would clash with the navigation property that EF would have created for the second table. 请注意,我无法命名扩展方法RelatedItems,因为这将与EF为第二张表创建的导航属性发生冲突。 That's perhaps not a good name for that tables, as it's not the actual items, rather the IDs of the items. 这可能不是该表的好名字,因为它不是实际的项目,而是项目的ID。 Either way, the above code should work. 无论哪种方式,上面的代码都应该起作用。

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

相关问题 实体框架:保存相关实体 - Entity Framework: saving related entities 实体框架实体sql vs linq to entities - entity framework entity sql vs linq to entities 实体框架:跳过几列或从相关实体中选择为空 - Entity Framework: Skip few columns or select as null from related entities 使用Entity Framework保存对SQL Server数据库的更改时,一个或多个实体的验证失败 - Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework 避免在带有Entity Framework导航属性的Sql Server表中使用自引用实体重复 - Avoid duplicates with self-referencing entities in Sql Server table with Entity Framework navigation property 如何使用原始 SQL 查询在 .net core 中检索具有相关实体的实体? - How to retrieve an entity with related entities in .net core with raw SQL queries? 一个实体关联多个实体 - One entity related to multiple entities 使用SQL和Linq(实体框架/实体)的多对多关系 - Many to Many relationship using SQL and Linq (entity framework/to entities) 在Entity Framework 4中执行本机sql并返回除已知实体以外的其他实体 - Executing native sql in Entity Framework 4 and returning other than known entities 实体框架v6.1有效加载深层相关实体,然后对其进行查询 - Entity Framework v6.1 Load Deep Related Entities Efficiently And Then Query On Them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM