59801

Entity Framework Include without Relations

Question:

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.

Answer1:

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>Update</strong>

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 good example of an Audit Manager is on <a href="http://www.codeproject.com/Articles/34491/Implementing-Audit-Trail-using-Entity-Framework-Pa" rel="nofollow">CodeProject</a>, where the author demonstrates a method to not only store audit information, but also roll back.

Recommend

  • else if statement in javascript not able to display validation message
  • Linked tables and Slicer in excel
  • How to filter entities that are deleted using linq to entities
  • Singleton Alternative - is it equivalent?
  • Polymer build not to create bundled and unbundled folder
  • WebApp in AppServices vs CloudService
  • ASP.NET, C# How to Pass a StringQuery to a custom SQL Command
  • How do i find all references to a user control
  • Mongodb update() vs. findAndModify() performace
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • TSQL Rolling Average of Time Groupings
  • Many to Many in Linq using Dapper
  • Multiple Left Join LINQ-to-entities
  • Action Pack components in Rails
  • Cypher - matching two different possible paths and return both
  • Creating a Multidimensional, Associative Array in VBScript
  • MySQL performance when updating row with FK
  • Exception creating JSON with LINQ
  • Linq Merge lists
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Loading .coffee files via a view in Rails
  • Is playing sound in Javascript performance heavy?
  • Create DicomImage from scratch using Dcmtk
  • Row Count Is Returning the incorrect number using RaptureXML
  • Atlas images wrong size on iPad iOS 9
  • NHibernate Validation Localization with S#arp Architecture
  • How to delay loading a property with linq to sql external mapping?
  • Insert into database using onclick function
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Bug in WPF DataGrid
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Knitr HTML Loop - Some HTML output, some R output
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Redux, normalised entities and lodash merge
  • how does django model after text[] in postgresql [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How to load view controller without button in storyboard?