entity framework - Delete row from many-to-many table (VB.Net, .edmx) -


i have users , regions. user can assigned number of regions.

to implement have table of users, table of regions, , third table userregion userid, regionid (both columns form primary key , have foreign key relationships user , region tables).

entity framework not import userregion table data model, instead creates property of each user object list of regions, , on each region object list of users. useful except can't figure out how un-associate user region.

the below code

dim db new databasecontext dim user = db.users.where(stuff).first() user.regions.clear() db.savechanges() 

produces error:

the operation failed: relationship not changed because 1 or more of foreign-key properties non-nullable. when change made relationship, related foreign-key property set null value. if foreign-key not support null values, new relationship must defined, foreign-key property must assigned non-null value, or unrelated object must deleted. 

how can rid of relationship rows don't want anymore?

i figured out.

the relationship needs removed both sides. code should be:

user.regions.clear() each r in db.regions     r.users.remove(user) next db.savechanges() 

now have zillion loops peppering function oh well. helps someone.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -