c# - EF - Part of composite keys in foreign key relationships -


i attempting utilise "code first database" feature of ado.net. generation works , outputs of desired models. have several ef models both have composite keys primary keys. i've given 2 such models example below.

public partial class labrole {     public labrole()     {         labrolepermissions = new hashset<labrolepermission>();         labuserroles = new hashset<labuserrole>();         rolesubscriptions = new hashset<rolesubscription>();     }      [key]     [column(order = 0)]     [stringlength(50)]     public string roleid { get; set; }      [key]     [column(order = 1)]     [stringlength(50)]     public string labid { get; set; }      public virtual icollection<labrolepermission> labrolepermissions { get; set; }      public virtual lab lab { get; set; }      public virtual icollection<labuserrole> labuserroles { get; set; }      public virtual icollection<rolesubscription> rolesubscriptions { get; set; } } 

and:

public partial class labrolepermission {     [key]     [column(order = 0)]     [stringlength(50)]     public string labid { get; set; }      [key]     [column(order = 1)]     [stringlength(50)]     public string roleid { get; set; }      [key]     [column(order = 2)]     [stringlength(50)]     public string permissionid { get; set; }      public virtual lab lab { get; set; }      public virtual labrole labrole { get; set; }      public virtual permission permission { get; set; } } 

this has foreign key relationship set on it:

modelbuilder.entity<labrole>()             .hasmany(e => e.labrolepermissions)             .withrequired(e => e.labrole)             .hasforeignkey(e => new { e.roleid, e.labid })             .willcascadeondelete(false); 

i run add-migration , initial migration script generated successfully. however, when try , access in context errors:

problem in mapping fragments starting @ lines 119, 128: foreign key constraint 'labrole_labrolepermissions' table labrolepermission (labid, roleid) table labrole (roleid, labid):: insufficient mapping: foreign key must mapped associationset or entitysets participating in foreign key association on conceptual side. 

could please explain why happening and/or solution problem.

ok have found answer. if remove use of data annotations specifying keys , instead specify them using fluent api generated database functions expected.

for example, if change models to:

public partial class labrole {     public labrole()     {         labrolepermissions = new hashset<labrolepermission>();         labuserroles = new hashset<labuserrole>();         rolesubscriptions = new hashset<rolesubscription>();     }      [stringlength(50)]     public string roleid { get; set; }      [stringlength(50)]     public string labid { get; set; }      public virtual icollection<labrolepermission> labrolepermissions { get; set; }      public virtual lab lab { get; set; }      public virtual icollection<labuserrole> labuserroles { get; set; }      public virtual icollection<rolesubscription> rolesubscriptions { get; set; } } 

and:

public partial class labrolepermission {     [stringlength(50)]     public string labid { get; set; }      [stringlength(50)]     public string roleid { get; set; }      [stringlength(50)]     public string permissionid { get; set; }      public virtual lab lab { get; set; }      public virtual labrole labrole { get; set; }      public virtual permission permission { get; set; } } 

then define keys using fluent i.e.

modelbuilder.entity<labrolepermission>().haskey(e => new { e.labid, e.roleid, e.permissionid });  modelbuilder.entity<labrole>().haskey(e => new { e.roleid, e.labid }); 

i can access database without issues. i'm not sure why works does. hope helps in future.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -