Wednesday 15 February 2012

How to work with multiple levels of abstractions with Entity Framework and code first? -


I have several models whose intermediate classes are abstract, the model looks fine, but I get an error when trying to create a database :

Problems in mapping pieces starting with line 13: Two entities with different keys are mapped to the same line. Ensure these two mapping pieces do not place institutions in the same group with two different keys in the same group of rows.

Here's the easiest code I can write to reproduce the error. If I make pet solid, the problem gets resolved What can I do to allow many abstract classes in my hierarchy? Public intangibles (public; id; get; set;}} public abstract class pet: animal {public string name {get; set;}} public squared fish: stomach {public bolt

 Public square dog: pet {public bool isiuddard {received; set;}} public class person: animal {public stomach MyPet {get; set;}} public class personaltext: DBConttex {public dbset & Lt; person & gt; people {received; set;}} [testfictions] public class animal tests {[test] public zero; CanCreateDatabase (Database.SetInitializer (new drop credits database always & lt; PersonContext & gt; ()); (Using the Var context = new PersonContext ()). (Assert.AreEqual (0, context.People.Count ()); // fails here}}}   

Update Here is a KDiff snap of the difference in the .edmx file generated. The file on the left is my original code which fails and occurs on the right when I have a DbSet

in my DbContext.
kdi of edmx difference

You need to add pets in your context:

  // Table-per-class (TPC) public subject subjective reference: DBcinTex {Public Personality} () {} Public DBSet & lt; Person & gt; People {receive; Set; } Public DbSet & lt; Pet & gt; Pets {receive; Set; }}   

Edit: I have just seen your comment, where you said that you wanted the table by the hierarchy inheritance. Your problem, then, is that your DBAet is not typed correctly - you want a single dBset using the base type:

  // Table-per-hierarchy (TPH) public class Subjective references: DBcinTex {Public Public Planning () {} Public DbSet & lt; Animals & gt; Animals {receive; Set; }}    

No comments:

Post a Comment