实体框架包括没有关系 [英] Entity Framework Include without Relations

查看:151
本文介绍了实体框架包括没有关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework的一个巨大的工程。因为我做了几乎所有的实体随时间变化的,不可能使用实体之间的关系了(我管理的关系,我自己)。
那时,上来的问题是,我不再能够容易地尽我所能之前查询数据。
说我有一个名为学生的实体。这个实体包含图书(列表)属于学生的集合。每本书包含了来自数据库以及一个author属性。
。要加载所有与他们的书的学生和通讯作者我要么必须写几百个自定义查询的指标(如LINQ),或导致大的性能影响我懒加载它们。
实际上这将是巨大的,如果我可以使用实体框架的Inlude无关系。是否有人知道如何实现这一目标?谢谢你。

I am working on a huge project using 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. This entity contains a collection of Books (List) which belong to the student. Each book contains an author property which comes from the database as well. To load all the students with theirs book and the corresponding authors I either have to write hundreds of custom queries (f.e. linq) or I lazy load them which results in a big performance impact. Actually it would be great if I could use the Inlude of Entity Framework without relations. Does anybody know how to achieve this? Thank you.

推荐答案

如果实体框架不管理表之间的关系,那么它就没有用的办法 .INCLUDE()这将创建连接的SQL调用。你最好的选择将是相关实体添加到父正常,但使用 [NotMapped] 属性。然后你可以使用自定义的SQL调用来执行查找,当你需要它们填充属性。然而,我很困惑,你的说法,由于实体是时间相关的,你不能使用关系,这只是外键在数据库......也许还有别的东西,你可以做些什么来优化你的实体。如果您正在管理项目之间你自己的关系,你卸载了很多的责任,从您的数据库到应用,这似乎是很多已经工作的。

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. Your best option would be to add the related entities to the parent as normal, but use the [NotMapped] Attribute. Then you can use a custom SQL call to perform your lookups and populate the properties when you need them. 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.

< STRONG>更新

实施跟踪审核是实体框架中一个相当标准的过程,可能比管理你的关系要容易得多。审计线索主要涉及具有用于 Context_SavingChanges 方法的事件处理程序(),然后通过 ObjectStateManager 。添加在这个循环中的逻辑,你也可以无效或拒绝添加/不属于某个特定的时间框架内更新。

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. Adding logic within this loop you could also invalidate or reject additions/updates that don't fall within a specific time frame.

审计经理的一个很好的例子是< A HREF =http://www.codeproject.com/Articles/34491/Implementing-Audit-Trail-using-Entity-Framework-Pa相对=nofollow> CodeProject上,在这里笔者演示的方法不仅存储审计信息,而且还回滚

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.

这篇关于实体框架包括没有关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆