sql - ASP MVC Delete only the relation between 2 tables (many-to-many relation) -
using asp mvc active record.
ive got 2 tables records related , aren't. relation defined user. 1 table has projects, other has devices. projects can created , deleted, devices cannot. when user deletes project, relations between project , devices should removed, devices should remain.
how do this?
my delete action looks this:
public actionresult delete(int id, formcollection collection) { if (!project.exists(id)) return redirecttoaction("index/1", "error"); try { project project = project.find(id); if (project.user.id != sessionvariables.authenticateduser.id) return redirecttoaction("index/1", "error"); project.deleteandflush(); return redirecttoaction("index", "project"); } catch(exception e) { return redirecttoaction("index", "error"); } }
so if understand correctly, when project deleted projects table want delete entries in projectdevice mapping table? (i assume using many many relationship 1 of questions).
if that's case, recommend use database trigger, rather try , manually in code. can create trigger removes rows specific project in mapping table, when project deleted project table. if you're using ms sql like:
create trigger trig_deleteprojectdevice on projecttable delete declare @projectid int select @projectid = (select projectid deleted) delete projectdevicemappingtable projectid = @projectid
Comments
Post a Comment