Code-First Entity Framework Multiple Collections with Many to Many -


i have entity framework question here. have complicated object called book , object has number of collections of type contributor such writer, letterer, colorist, etc. contributors not scoped particular role though. same contributor (with same contributorid) both writer , colorist, example.

public book {         public icollection<contributor> writers { get; set; }         public icollection<contributor> artists { get; set; }         public icollection<contributor> pencilers { get; set; }         public icollection<contributor> inkers { get; set; }         public icollection<contributor> colorists { get; set; }         public icollection<contributor> letterers { get; set; }         public icollection<contributor> coverartists { get; set; }         public icollection<contributor> othercontributors { get; set; } }  public contributor {      public int contributorid { get; set; }      public string name { get; set; } } 

i having trouble, viewing examples have found here , on other sites, determining how signify appropriate model. expect db model this. want avoid model wherein have separate table every contributor role, or separate row in contributor table every instance in contributor associated book in role.

+ books       --bookid + contributors       --contributorid + bookcontributors       --bookid       --contributorid       --discriminator 

i such ado.net guy not finding enjoyable, determined become @ least borderline proficient in important framework.

a quick note: since opening question, got pulled away @ work , haven't had time thoroughly review answers , play around results. didn't want leave bounty hanging appreciate answers has provided. selected answer appeared of interest me starting out. want thank though this.

i have worked on solution implements model proposed although works bit different expect. hope answers question.

models

[table("book")] public class book {     [column("bookid")]     [databasegenerated(databasegeneratedoption.identity)]     public int bookid { get; set; }      [notmapped]     public icollection<contributor> writers { get; set; }      [notmapped]     public icollection<contributor> artists { get; set; }      [notmapped]     public icollection<contributor> pencilers { get; set; }      [notmapped]     public icollection<contributor> inkers { get; set; }      [notmapped]     public icollection<contributor> colorists { get; set; }      [notmapped]     public icollection<contributor> letterers { get; set; }      [notmapped]     public icollection<contributor> coverartists { get; set; }      [notmapped]     public icollection<contributor> othercontributors { get; set; } }  [table("contributor")] public class contributor {     [column("contributorid")]     [databasegenerated(databasegeneratedoption.identity)]     public int contributorid { get; set; } }  // contributor type 1 of following options: writer, artist, penciler, etc. [table("contributortype")] public class contributortype {     [column("contributortypeid")]     [databasegenerated(databasegeneratedoption.identity)]     public int contributortypeid { get; set; }      [column("name")]     public string name { get; set; } }  [table("bookcontributor")] public class bookcontributor {     [column("bookcontributorid")]     [databasegenerated(databasegeneratedoption.identity)]     public int bookcontributorid { get; set; }      [column("bookid")]     public int bookid { get; set; }      [column("contributorid")]     public int contributorid { get; set; }      [column("roleid")]     public int roleid { get; set; }      [foreignkey("bookid")]     public virtual book book { get; set; }      [foreignkey("contributorid")]     public virtual contributor contributor { get; set; }      [foreignkey("roleid")]     public virtual contributortype role { get; set; } } 

database context

appdbcontext.cs:

public class appdbcontext : dbcontext {     public appdbcontext()     {         database.setinitializer<appdbcontext>(new appdbinitializer());     }      public appdbcontext(string connectionstring)         : base(connectionstring)     {         database.setinitializer<appdbcontext>(new appdbinitializer());     }      public dbset<book> books { get; set; }      public dbset<contributor> contributors { get; set; }      public dbset<contributortype> contributortypes { get; set; }      public dbset<bookcontributor> bookcontributors { get; set; } } 

appdbinitializer.cs:

public class appdbinitializer : dropcreatedatabasealways<appdbcontext> {     protected override void seed(appdbcontext context)     {         // default contributor types         var contributortypes = new list<contributortype>();         contributortypes.add(new contributortype() { name = "writer" });         contributortypes.add(new contributortype() { name = "artist" });         contributortypes.add(new contributortype() { name = "penciler" });         contributortypes.add(new contributortype() { name = "inker" });         contributortypes.add(new contributortype() { name = "colorist" });         contributortypes.add(new contributortype() { name = "letterer" });         contributortypes.add(new contributortype() { name = "coverartist" });         contributortypes.add(new contributortype() { name = "othercontributor" });          // adding context         foreach (var type in contributortypes)             context.contributortypes.add(type);          base.seed(context);     } } 

wrapping together

program.cs:

class program {     static void main(string[] args)     {         // enter name of connection string in app.config file         var connectionsettings = configurationmanager.connectionstrings["..."];         using (var dbcontext = new appdbcontext(connectionsettings.connectionstring))          {             // creating book             var book = new book();             dbcontext.books.add(book);             dbcontext.savechanges();              // creating contributor             var contributor = new contributor();             dbcontext.contributors.add(contributor);             dbcontext.savechanges();              // adding contributor book             var bookcontributor = new bookcontributor()             {                 bookid = book.bookid,                 contributorid = contributor.contributorid,                 roleid = dbcontext.contributortypes.first(t => t.name == "writer").contributortypeid             };             dbcontext.bookcontributors.add(bookcontributor);             dbcontext.savechanges();              // retrieving book             var book = dbcontext.books.where(b => b.bookid == 2).firstordefault();             if (book != null)              {                 book.writers =                      contributor in dbcontext.contributors                     join bookcontributor in dbcontext.bookcontributors on contributor.bookid equals bookcontributor.bookid                     join contributortype in dbcontext.contributortypes on contributortype.contributortypeid equals bookcontributor.contributortypeid                                              bookcontributor.bookid == 2 ,                         contributortype.name == "writer"                     select contributor;                  // same other types of contributors             }         }     } } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -