Note that there are some explanatory texts on larger screens.

plurals
  1. POEF5: how to return only Foreign Keys?
    primarykey
    data
    text
    <p>I'm using EF5 code-first in combination with WCF in an N-Tier application.</p> <p>I'm asynchronously and incrementally loading related entities between client and server. All is working well, but I'd like to do some optimization.</p> <p>Consider a fictitious entity <code>Car</code>, with a related entity <code>Wheel</code>:</p> <pre><code>public class Car { public int Id { get; set; } public virtual List&lt;Wheel&gt; Wheels { get; set; } } public class Wheel { public int Id { get; set; } public virtual int CarId { get; set; } } </code></pre> <p>And the related DTO:</p> <pre><code>public class CarDTO { public int Id { get; set; } public virtual int CarId { get; set; } public virtual List&lt;int&gt; Wheels { get; set; } } </code></pre> <p>Note that <code>Wheels</code> is a list of foreign keys in the DTO. I don't need to necessarily transfer every <code>Wheel</code> object through the web service - the client will load it in a later WCF call, if needed. Right now I'm using AutoMapper to flatten the related entities down into a list of FKs.</p> <p>The trouble with this is that my web service doesn't always need to load the entire Wheel object from the <code>DbContext</code> when retrieving a <code>Car</code> object. I don't send them across the web service anyway (unless later asked for them). But the queries load the entire wheel object from the database into the web service call, only to be discarded. If there are 100 <code>Wheels</code> per <code>Car</code>, that's a significant amount of unnecessary data flowing between the web service and the database.</p> <p>What I would like to do is change <code>Car</code> to something like this:</p> <pre><code>public class Car { public int Id { get; set; } public virtual List&lt;Wheel&gt; Wheels { get; set; } // Normally empty public virtual List&lt;int&gt; WheelIDs { get; set; } // Normally full } </code></pre> <p>So when the web service knows that it needs to load all of the <code>Wheels</code>, it can do so by adding <code>.Include(car =&gt; car.Wheel)</code> to the query, but normally it simply returns a list of FKs in <code>WheelIDs</code> that can be retrieved later.</p> <p>Will EF5 do this easily? If so, how?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload