c# - Entity Framework Multiple Collections of Same Type -


i trying create class 2 collections of same type. when read 1 of collections, data both. means, there must wrong fluent api configuration.

in linqpad, can see collections "alternatives" , "answers" has been combined single "pageelements"-collection.

sample code

class program {     static void main(string[] args)     {         var alternatives = new list<pageelement>             {                 new pageelement {data = "alternative 1"},                 new pageelement {data = "alternative 2"},                 new pageelement {data = "alternative 3"},                 new pageelement {data = "alternative 4"},             };          var answers = new list<pageelement>             {                 new pageelement { data = "answer 1" },                 new pageelement { data = "answer 2" },             };          var page = new page             {                 alternatives = alternatives,                 answers = answers             };           using (var context = new mycontext())         {             context.pages.add(page);             context.savechanges();              var loadedpage = context.pages.first();              foreach (var answer in loadedpage.answers)             {                 console.writeline("answer: " + answer.data);             }          }           console.readline();     } }    public class mycontext : dbcontext {     public dbset<page> pages { get; set; }      public mycontext()     {      }      protected override void onmodelcreating(dbmodelbuilder mb)     {          mb.entity<pageelement>()           .hasrequired(p => p.page)           .withmany(p => p.alternatives)           .hasforeignkey(p => p.pageid);          mb.entity<pageelement>()           .hasrequired(p => p.page)           .withmany(p => p.answers)           .hasforeignkey(p => p.pageid);      }  }  public class page {     public int id { get; set; }     public virtual icollection<pageelement> alternatives { get; set; }     public virtual icollection<pageelement> answers { get; set; } }  public class pageelement {     public int id { get; set; }     public int pageid { get; set; }     public virtual page page { get; set; }      public string data { get; set; } } 

output

answer: alternative 1 answer: alternative 2 answer: alternative 3 answer: alternative 4 answer: answer 1 answer: answer 2 

desired output

answer: answer 1 answer: answer 2 

this because both alternatives , answers stored in single table pageelements - though construct them seperately, once hit database saved together. because of this, when reloaded, ef has no way split them 2 groups.

a simple solution split them 2 types, eg alternativeelement , answerelement therefor create 2 tables, , avoid problem.

edit

to answer follow on question: follow pattern of derived classes, if subclasses don't add new fields. use table per hierarchy pattern, have single database table, has column indicate subclass each row belongs to., so:

public class pageelement {     public int id { get; set; }     public int pageid { get; set; }     public virtual page page { get; set; }      public string data { get; set; } }  public class pageelementanswer : pageelement  { }  public class pageelementalternative : pageelement  { }  public class page {     public int id { get; set; }     public virtual icollection<pageelementalternative> alternatives { get; set; }     public virtual icollection<pageelementanswer> answers { get; set; } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -