Entity Framework many to many fluent API -


i'm working existing database schema fixed.

there 3 many-to-many relations, contact-group, contact-department , contact-team. there common table, contactrelation acts middle table 3 relations.

public class contact {     [column("ctcid")]     public int id { get; set; }      [column("ctcfirstname")]     public string firstname { get; set; }     [column("ctcfamilyname")]     public string lastname { get; set; }      public virtual icollection<contactrelation> grouprelations { get; set; }     public virtual icollection<contactrelation> departmentrelations { get; set; }     public virtual icollection<contactrelation> teamrelations { get; set; } }  public class group {     [key, column("grid")]     public int id { get; set; }     [column("grname")]     public string name { get; set; }      public virtual icollection<contactrelation> contactrelations { get; set; } }  public class department {     [key, column("depid")]     public int id { get; set; }      [column("depname")]     public string name { get; set; }      public virtual icollection<contactrelation> contactrelations { get; set; }   }  public class team {     [key, column("tmid")]     public int id { get; set; }     [column("transcode")]     public string transcode { get; set; }      public virtual icollection<contactrelation> contactrelations { get; set; }  }  public class contactrelation {     [key]     [column("ctcrelnid")]     public int id { get; set; }      [column("grid")]     public int groupid { get; set; }      [column("depid")]     public int departmentid { get; set; }      [column("tmid")]     public int teamid { get; set; }      [column("cuctcid")]     public int contactid { get; set; }      [column("rcode")]     public string rolecode { get; set; } } 

in mapping, have following code:

    protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.entity<group>()             .hasmany(g => g.contactrelations)             .withrequired()             .hasforeignkey(r => r.groupid);          modelbuilder.entity<department>()             .hasmany(c => c.contactrelations)             .withrequired()             .hasforeignkey(r => r.departmentid);          modelbuilder.entity<team>()             .hasmany(s => s.contactrelations)             .withrequired()             .hasforeignkey(r => r.teamid);          modelbuilder.entity<contact>()             .hasmany(c => c.grouprelations)             .withrequired()             .hasforeignkey(r => r.contactid);          modelbuilder.entity<contact>()             .hasmany(c => c.departmentrelations)             .withrequired()             .hasforeignkey(r => r.contactid);          modelbuilder.entity<contact>()             .hasmany(c => c.teamrelations)             .withrequired()             .hasforeignkey(r => r.contactid);     } 

i try execute following query:

            var ctc = repo.contacts                 .include("grouprelations")                 .include("departmentrelations")                 .firstordefault(c => c.firstname.tolower() == "jason"); 

and keep getting error:

system.data.sqlclient.sqlexception:  invalid column name 'contact_id1'. invalid column name 'contact_id'. invalid column name 'contact_id1'. invalid column name 'contact_id2'. 

i read somewhere table cannot participate in more 1 many-to-many relations. true? because contactrelation table used more once i'm getting error?

if so, what's correct way map these relations, without modifying database schema.?

ps: i'm working ef6.1

thanks help.

remove mapping

 modelbuilder.entity<contact>()             .hasmany(c => c.teamrelations)             .withrequired()             .hasforeignkey(r => r.contactid); 

and try executing query.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -