Note that there are some explanatory texts on larger screens.

plurals
  1. POEntityFramework Code First and custom Inheritance
    primarykey
    data
    text
    <p>We have a large enterprise application running successfully using classic ADO.NET techniques. We are experimeting with EF 6 to see if we can reduce some of the plumbing required with classic techniques (please do not answer with pros and cons of any of these, we have done our homework).</p> <p>We have decided to use EF Code First approach as it gels best with our existing code. We are stuck at representing a particular Inheritance scheme in our EF model classes.</p> <p>There's a table called Profiles, which has ProfileId (Identity) and ProfileNumber columns. The underlying details are a bit complex, but to try putting that in simpler words, ProfileId is a primary key. ProfileNumber can be unique or not based on the type of profile. e.g. for agent profiles, ProfileNumber cannot repeat in Profiles table.</p> <p>Now there's another table called AgentDetails having ProfileNumber as foreign key into Profiles table (there's no ProfileId column in AgentDetails table). Please do not suggest schema changes, that's simply not feasible with the kind of deployments we have.</p> <p>Now we have created a Profile class in our code with all the properties. We have created another class called Agent that derives from Profile. It has properties for all the columns in AgentDetails table except ProfileNumber as that is inherited from Profile class.</p> <p>We are trying to write a LINQ query that returns us agent objects where all the Profile class derived properties are also populated. However after trying a variety of LINQ queries (with and without explicit joins), none seems to succeed. We always get an error on ProfileId column which indicates the result set from LINQ query did not contain the ProfileId column.</p> <p>Is it possible to use DbContext and DbSet and Linq queries in this situation to write a LINQ expression which joins AgentDetails with Profiles table on ProfileNumber, and returns a List of agent objects where the underlying Profile properties are also populated.</p> <p>We have tried with various DataAnnotation attributes (but haven't looked into Fluent API) for getting this done but haven't been successful.</p> <p>Please note we do not wait Agent object to contain the Profile object, we want it to inherit from Profile. <a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx" rel="nofollow">This blog post</a> demonstrates the same when relationships have been defined in database explicitly. In our case, there's no foreign key relation defined in database between AgentDetails and Profiles table.</p> <p>Off-course, one way is to use a DataReader in this case, and manually populate the object properties. We are trying to see if there's a native EF approach way of getting this done, else we might look into delegating to good old data readers.</p> <p>UPDATE: Here is the skeleton version of code:</p> <pre><code>Table("Profiles")] public class Profile { public int ProfileId {get; set;} public string ProfileNumber {get;set;} public string FullName {get;set;} } Table("AgentDetails")] public class Agent : Profile { //Notice I omitted ProfileNumber column, that's present in AgentDetails in database, but inherited from Profile class here. public decimal CommissionPct {get;set;} } </code></pre> <p>Here's a sample query:</p> <pre><code>//Basically trying to get a list of all agents this.context.Agents.ToList(); </code></pre> <p>And the error we get is:</p> <blockquote> <p>Invalid column ProfileId</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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.
 

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