Deleting from Many-to-Many relationships in EntityFramework 4

I am attempting to delete a series of entities using EntityFramework 4. Here is the code I am using:

var role = (
    from r in context.tblAdminRoles
    where r.AdminRoleId == this.Role.AdminRoleId
    select r
).First();

this.AdminUser.tblAdminRoles.Remove(role);
context.SaveChanges();

context.tblAdminRoles.Remove(role);
context.SaveChanges();

However, when I execute it, I get the following error:

The DELETE statement conflicted with the REFERENCE constraint "FK_tblAdminUserRole_tblAdminRole". The conflict occurred in database "MyMainSite2", table "dbo.tblAdminUserRole", column 'AdminRoleId'.

The statement has been terminated.

My database has the following structure:

----------------      --------------------      ----------------
|              |      |                  |      |              |
| tblAdminUser | ---< | tblAdminUserRole | >--- | tblAdminRole |
|              |      |                  |      |              |
----------------      --------------------      ----------------

Can anyone point me in the right direction about what might be going wrong?

Answers


Check if cascading delete is enabled in the database for the two relationships refering to the link table tblAdminUserRole, especially for the FK_tblAdminUserRole_tblAdminRole to the tblAdminRole table. It looks like it isn't enabled, therefore deleting the role doesn't delete the entries in the link table which finally leads to the FK constraint violation.


Need Your Help

How do disk controllers handle concurrent writes to same sector in absence of write barriers?

linux-kernel non-deterministic aio concurrentmodification

When I open a file with O_DIRECT|O_ASYNC and do two concurrent writes to the same disk sector, without a fsync or fdatasync in between, does the linux disk subsystem or the Hardware disk controllers

Clone in jQuery number of times?

javascript jquery

I am setting an item to localStorage with the syntax of #id, #id+number of times div has been duplcated. The reason for this is that I have a button that .clone()'s the div as many times as the user