简体   繁体   中英

Entity FrameWork , many to many realtion , reading data from navigation properties of Entity

I am working on asp.net web forms (C#) , with mysql and entity framework. There in my database , a User table is with many to many relationship with permission table , and a third table UserPermission to establish this relation. Following are the classes generated by entity framework data model.

 public class User{
 public int Serial { get; set; }
 public string User_Name { get; set;
 public virtual ICollection<UserPermission> UserPermission { get; set; }
 }
 public class Permission{
 public int Serial { get; set; }
 public string Name { get; set;
 public virtual ICollection<UserPermission> UserPermission { get; set; }
 }
 public partial class UserPermission
 {
    public int Serial { get; set; }
    public int USER_Sr { get; set; }
    public int Permission_Sr { get; set; }

    public virtual User User { get; set; }
    public virtual Permission Permission { get; set; }
}

Now my problem is , that based on a user id , i want read the permissions it's associated with. I want to have users with their assigned permissions , populate these into a gridview , where each row shows the record of a user , and there a combobox in that row contains the permissions , that user has gain.

Thank you very much for your help.

To get permissions assigned to a user

var userPermissions = MyContext.Users.Find(userId).Permissions();

Where MyContext is your DbContext and Users is defined as DbSet in this context. You didn't specify what you are using to display information, so I can't help you on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM