19156

EF Table Per Type with Navigation property on Sub Type Table

Question:

I'm hoping someone out in the SO community will be able to help me out here.

Simplified Background: I'm using Entity Framework V1 to build my class structure that is outlined below, I'm using Table Per Type to persist my inherited objects:

Employee CaseA : Case CaseB : Case CaseC : Case

<em>CaseB has a Navigational Property to Employee</em>

I have a Repository that returns an ObjectQuery. If the type of Case is actually CaseB, I need to include the Employee object within the graph. I can't .Include("Employee") because it's not a navigational property of Case, and Employee doesn't have a .Load() method on it.

Ideally I want to be able to do this in one query, however as a fall back I'm happy that I make a call, check the Object and perform another call, something like this: (although as I stated earlier, load doesn't exist on the employee navigational property)

//Get the case from the Case myCase = new Repo<Case, Entities>.FirstOrDefault(); if(myCase is CaseB) ((CaseB)myCase).Employees.load();

Am I missing something really simple here?

Answer1:

Try this:

var employee = ctx.Cases .OfType<CaseB>() .Include("Employees") .Select(x => x.Employees) .FirstOrDefault();

OfType<T>() is one of the most important methods in EF when it comes to inheritance - you should familiarize yourself with it.

Essentially is filters the items in the query to be of a particular type - very similar to the conditional check your doing in your answer.

It's an IQueryable method (LINQ-Objects), but in LINQ-Entities (ObjectQuery<T>), it get's implemented as an INNER JOIN.

The above should work - just make sure you do the eager load <em>after</em> you do the OfType.

HTH.

Answer2:

As always, after posting the question I found <a href="https://stackoverflow.com/questions/1162313" rel="nofollow">this</a> and <a href="https://stackoverflow.com/questions/944332/entity-framework-inheritance-and-include" rel="nofollow">this</a> which has pointed me towards using projection (solution below), but I was hoping to avoid that, so I'm going to leave the question open to see if there is a more elegant solution.

var Employee = caseObjectQuery.Select(x => new{ Employee = x is CaseB ? (x as CaseB).Employee : null } ).FirstOrDefault();

Just by selecting the Object into memory, EF magically maps the related entities.

Recommend

  • Android: How to set status bar and navigation bar semi transparent
  • Neo4j .NET Client - Getting a Path from a Node to its Root
  • Navigation within section of a page using react router
  • How to query specific adapters in Win32_PerfFormattedData_Tcpip_NetworkInterface?
  • Help required on onbeforeunload or click on browser back button
  • K Shortest Path Python Not Working
  • Rely on Facebook user id as a permanent user identifier
  • How to add learning rate to summaries?
  • Double dispatch in Java example
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Loading .coffee files via a view in Rails
  • Create DicomImage from scratch using Dcmtk
  • Database structure design with variable amounts of fields
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Trying to switch camera back to front but getting exception
  • How to make Safari send if-modified-since header?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Akka Routing: Reply's send to router ends up as dead letters
  • How to pass list parameters for each object using Spring MVC?
  • AT Commands to Send SMS not working in Windows 8.1
  • Buffer size for converting unsigned long to string
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Binding checkboxes to object values in AngularJs
  • How to load view controller without button in storyboard?