vb.net - Deleting Many-To-Many entry -
how can 1 delete many-to-many entry? have entities client
, restaurant
, relationship table , entity between them: clientrestaurant
having next columns: clientid
, restaurantid
, dateofbill
. client entity has navigational property clientrestaurants
list of clientrestaurant
. when delete entry client
's clientrestaurants
list:
myclient.clientrestaurants.remove(myclientrestaurants)
and seek savechanges
next error:
adding relationship entity in deleted state not allowed.
update: set columns in clientrestaurants table sql server nullable. if do:
myclient.clientrestaurants.remove(myclientrestaurants) myrestaurant.clientrestaurants.remove(myclientrestaurants)
and savechanges
works fine, mean entries relationships removed, in db remains row null
values, , since have unique index
2 columns, can have 1 row null
values. there has solution, eludes me. help appreciated. these functions add together , remove records intermediary table.
private function addtehniciantolucrare(l lucrare, t tehnician) tehnicianlucrare dim result tehnicianlucrare = nil dim tl new tehnicianlucrare tl.tehnician = t 't.lucraritehnician.add(tl) tl.lucrare = l 'l.tehnicienilucrare.add(tl) if tl isnot nil result = tl homecoming result end function private function removetehnicianfromlucrare(l lucrare, t tehnician) tehnicianlucrare dim result tehnicianlucrare = nil dim tl tehnicianlucrare = me._ctx.tehnicienilucrare.local.where(function(p) p.tehnician t , p.lucrare l).singleordefault if tl isnot nil me._ctx.tehnicienilucrare.remove(tl) result = tl end if homecoming result end function
consider lucrare restaurant , tehnician client above description.
update: have these subs:
private sub list_tehnicieniraport_doubleclick(sender object, e eventargs) handles list_tehnicieniraport.doubleclick seek if me.list_tehnicieniraport.selectedindex > -1 dim curteh = trycast(me.list_tehnicieniraport.selecteditem, tehnicianlucrare) if curteh isnot nil 'setproperty("tehnicienilucrare", curteh.tehnician) movetodatatable(curteh.tehnician) 'changetracker.haschanges() gives error here end if end if grab ex exception logerror(ex) end seek end sub public sub movetodatatable(byref ptehnician tehnician) seek setproperty("tehnicienilucrare", ptehnician) if getproperty("edit") selectechipa() selectcar() grab ex exception logerror(ex) end seek 'changetracker.haschanges() not give error here. end sub
this way error, , appears right after movetodatatable()
sub exits. had step-by-step debugging , calling _ctx.changetracker.haschanges()
after every step. if move setproperty("tehnicienilucrare", curteh.tehnician)
list_tehnicieniraport_doubleclick(...)
sub, don't error, , works fine. don't understand.....
excerpt question:
having next columns: clientid, restaurantid, dateofbill.
i feeling intermediary table not automatically generated ef, rather separate table individually defined. main reasoning behind you're adding dateofbill
, means you're manually inserting items in table.
it might deed intermediary table, ef doesn't see way.
if set many-to-many via ef (i.e. not managed you, autogenerated), there still table, hidden behind orm. code snippet applies scenario. in case, need remove relation; , internal table row deleted ef. because table part of describing relation.
but if intermediary table separate table defined yourself, entity; , need handle handle other entity. the table defined table, , has 2 relations, client , restaurant.
remove clientrestaurant
entity other entity:
mycontext.clientrestauranttable.remove(myentitytoremove);
don't worry relations. contained in clientrestaurant row (clientid, restaurantid), there won't referential issue.
the key part maintain in mind here there difference between tables ef autogenerates you, , intermediary table set (in cases, done when need additional columns in table, dateofbill
)
vb.net entity-framework
No comments:
Post a Comment