3
votes

I am using MVC 4 and C# 4.5 with EntityFramework 4.

I have setup a simple Many to Many table:

tblAdminUser -> tblAdminUserRole <- tblAdminRole

When I try to add a role to the admin user, I get the following error:

"Unable to update the EntitySet 'tblAdminUserRole' because it has a DefiningQuery and no element exists in the element to support the current operation."

The code I am using is:

this.Role = new tblAdminRole()
{
    Name = "__role__",
};

context.tblAdminRoles.Add(this.Role);
context.SaveChanges();

this.AdminUser.tblAdminRoles.Add(this.Role);
context.SaveChanges();
2

2 Answers

7
votes

Update your database, set pair of foreign key to tables in m2m table as primary key. Then update your model to database.

4
votes

Please check, if all ID fields in tblAdminUserRole have been set as Primary Key in your SQL database. Then update your model.

If Entity Framework can't figure out the primary key, it will generate a SELECT statement but it won't be able to create the according INSERT, UPDATE and DELETE statements.