c# - EF insert in link table creates entries in one of the parent tables -


this situation: - table user (id, name) - table role (id, name) - link table usersroles (iduser, idrole)

in link table iduser , idroles fks , both of them marked composite primary key.

in ef produces following models:

    public class user {         public int id {get;set;}         public string name {get;set;}         public icollection<role> roles {get;set;}    }      public class role{         public int id {get;set;}         public string name {get;set;}         public icollection<user> users{get;set;}    } 

when inserting role user strange thing happens.

user.roles.add(role); context.savechanges(); 

creates entry in usersroles table , adds role in role table. practically duplicates role trying associate user.

is ef related? because fk , pk settings tables?

any hint appreciated.

does role adding have id value of role exists. if has name ef may add new entry database.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -