0

I have a sql server database and C# kendo grids. The tables are Users,Roles,User_Roles. user table has userid as a primary key and Roles table has RoleId as a primary key. User_Roles have both roleid and user id as primary key. When i am trying to add the tables to the entity framework by using the option update from the database i can see both the user and roles table but not the User_Roles table because of the many to many relationship.

The issue is i want to access this table(User_roles) in the linq query to update the user when the roles are updated. But i am unable to access this table. Is there any way i can access this table or add to the entity framework.

The data are displaying on the kendo grids

Thanks

1 Answers1

1

Try using the Roles navigation property of each User entity.

e.g (if using LinqToEntities)

context.Users.Select(x => x.Roles)
Dienér
  • 109
  • 1
  • 9
  • No i dont see that table when i select its only showing x.Roles not the User_Roles since its not in the entity – user3916062 Jan 29 '16 at 21:51
  • x.Roles is the navigation property of the User, representing the Roles of a User. If you add or remove entities from x.Roles, the Roles of that specific User will be updated accordingly. – Dienér Jan 29 '16 at 21:58
  • So instead of using the User_Roles i can use Roles which will automatically apply for User_Roles – user3916062 Jan 29 '16 at 22:06
  • If I understand your setup correctly. Firstly, you should have two properties on your context (context.Roles and context.Users) representing the Users and the Roles in the db. Additionally, each User should have the Roles navigation property (user.Roles) representing the Roles of a specific User. You can add/remove from this navigation property, and that will affect the User_Roles. EF will never show relationship-tables (such as User_Roles), it is represented as navigation properties on either end of the relationstip (ie the User or the Role). – Dienér Jan 29 '16 at 22:20
  • You are correct. My current situation is on one grid i am displaying list of roles from Roles table. When i edit on a role it has few tabs. On one tab it diplays the list of all users from the user table along with a checkbox which is autogenerated and this check box shows whether that user have that role or not. So what i want to do is i want to assign the current role to one of the users and click update. So the method that needs to call should use the User_Roles table. – user3916062 Jan 29 '16 at 22:24
  • Yes, just make changes to the Roles property of the user object/entity. And these changes will be applied to User_Roles. – Dienér Jan 29 '16 at 22:35
  • Can you give me an example query – user3916062 Feb 03 '16 at 22:40
  • This question is marked as duplicate. Here is an example from the other thread/question: using (var context = new MyObjectContext()) { var user = context.Users.Single(u => u.UserId == 3); var role = context.Roles.Single(r => r.RoleId == 5); user.Roles.Add(role); context.SaveChanges(); } – Dienér Feb 03 '16 at 22:43
  • yah i tried the same Query .updated the question can you check it . The error i am getting is A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies'. – user3916062 Feb 03 '16 at 22:50