简体   繁体   English

实体框架包含而没有关系

[英]Entity Framework Include without Relations

I am working on a huge project using Entity Framework. 我正在使用Entity Framework进行大型项目。 Because I made almost all entities time-dependent it is not possible to use relations between entities anymore (I manage the relations myself). 因为我几乎使所有实体都依赖时间,所以不再可以使用实体之间的关系(我自己管理关系)。 The problem that came up is that I am not longer able to query data as easily as I could before. 出现的问题是我不再能够像以前那样容易地查询数据。 Say I have an entity called Student . 假设我有一个名为Student的实体。 This entity contains a collection of Books (List) which belong to the student. 该实体包含属于学生的Books (列表)的集合。 Each book contains an author property which comes from the database as well. 每本书都包含一个来自数据库的author属性。 To load all the students with theirs book and the corresponding authors I either have to write hundreds of custom queries (fe linq) or I lazy load them which results in a big performance impact. 要加载所有学生的书和相应的作者,我要么必须编写数百个自定义查询(fe linq),要么我懒加载它们,这会对性能产生很大影响。 Actually it would be great if I could use the Inlude of Entity Framework without relations. 实际上,如果我可以在没有关系的情况下使用实体框架的Inlude,那就太好了。 Does anybody know how to achieve this? 有人知道如何实现这一目标吗? Thank you. 谢谢。

If Entity Framework is not managing the relations between the tables, then it has no way of using .Include() which would create joins in the SQL call. 如果Entity Framework不在管理表之间的关系,则它无法使用.Include()在SQL调用中创建联接。 Your best option would be to add the related entities to the parent as normal, but use the [NotMapped] Attribute. 最好的选择是像往常一样将相关实体添加到父级,但是使用[NotMapped]属性。 Then you can use a custom SQL call to perform your lookups and populate the properties when you need them. 然后,您可以使用自定义SQL调用执行查询,并在需要时填充属性。 I am confused, however, by your statement that because the entities are "time-dependent" that you can't use relationships, which are simply Foreign Keys in the database... Perhaps there's something else you can do to optimize your entities. 但是,我对您的陈述感到困惑,因为这些实体是“时间相关的”,因此您不能使用关系,这些关系只是数据库中的外键...也许还有其他方法可以优化您的实体。 If you are managing your own relationships between items, you are offloading a lot of the responsibility from your DB into your app, which seems like a lot of work already. 如果您要管理项目之间的关系,那么您会将很多责任从数据库转移到应用程序中,这似乎已经是很多工作。

Update 更新

Implementing an Audit Trail is a fairly standard process in Entity Framework, and probably much easier than managing your relationships. 在实体框架中,实施审计跟踪是一个相当标准的过程,可能比管理您的关系要容易得多。 An audit trail basically involves having an event handler for the Context_SavingChanges() method, and then working with your new data and old data through the ObjectStateManager . 审核跟踪基本上涉及为Context_SavingChanges()方法提供事件处理程序,然后通过ObjectStateManager新数据和旧数据。 Adding logic within this loop you could also invalidate or reject additions/updates that don't fall within a specific time frame. 在此循环中添加逻辑,您还可以使不在特定时间范围内的添加/更新无效或被拒绝。

A good example of an Audit Manager is on CodeProject , where the author demonstrates a method to not only store audit information, but also roll back. 审计管理器的一个很好的例子是在CodeProject上 ,作者在其中演示了一种不仅存储审计信息而且回滚的方法。

如果实体之间没有主键和外键关系,则不能使用Include方法。

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

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