entity framework - EF 6.1 Bidirectional mapping returning null results from ICollection member -


i have set of allowed roles, , users can belong number of roles.

i need query both number of roles, , know within user entity roles belongs.

the first query works, second query not (no roles returned).

my code below:

class user {     [key, databasegenerated(databasegeneratedoption.identity)]     public guid id { get; set; }     [index(isunique=true)]     public string username { get; set; }      public icollection<role> roles { get; set; }      public user()     {         this.roles = new list<role>();     } }  class role {     [key]     public int id { get; set; }      [index(isunique = true)]     public string name { get; set; }        public icollection<user> users { get; set; } }  class membercontext : dbcontext {     public dbset<user> users { get; set; }     public dbset<role> roles { get; set; } } 

for below example ef has created 3 databases:

**roles {id,name}** 1, identityserverusers 2, identityserveradministrators  **userroles {user_id,role_id}** 5179ef31-c7dd-e311-80bd-00155d458501, 2  **users {id,name}** 5179ef31-c7dd-e311-80bd-00155d458501, admin 

i want able run following queries:

using(var context = new membercontext()) {     var roles = context.roles.where(x => x.name == "identityserveradministrators").firstordefault();     // roles.users empty!!!      var user = context.users.where(x => x.username == "admin").firstordefault();     // user.roles empty!! } 

but roles.users , user.roles both return empty, looking @ above database tables should have data.

do need explicitly set mapping via ef code first , doing wrong please?

update 1

i using virtual properties, still null's being returned:

class role {     [key]     public int id { get; set; }      [maxlength(127)]     [index(isunique = true)]     public string name { get; set; }             public virtual icollection<user> users { get; set; } }  class user {     [key, databasegenerated(databasegeneratedoption.identity)]     public guid id { get; set; }     [index(isunique = true)]     [maxlength(127)]     public string name { get; set; } }   using (var context = new membercontext()) {     var unf1 = context.roles.where(x => x.name == "identityserveradministrators").firstordefault();     var wow1 = unf1.users;     // roles.users empty!!!      var unf2 = context.users.where(x => x.username == "admin").firstordefault();     var wow2 = unf2.roles;     // user.roles empty!! } 

in queries, need explicitly load related entities since you're not using lazy loading.

using(var context = new membercontext()) {     var roles = context.roles.include(x => x.users).where(x => x.name == "identityserveradministrators").firstordefault();     // roles.users empty!!!     var user = context.users.include(x => x.roles).where(x => x.username == "admin").firstordefault();     // user.roles empty!! } 

if want related entities lazy loaded, navigation properties must virtual.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -