entity framework - How to handle cascading deletes on inherited tables? -


i've run problem on delete cascade when using inherited tables.

i've got following tables (model first) database model

now if want delete entry in entity1.

using (var ctx = new myentities()) {    var first = ctx.entity1set.first();    ctx.deleteobject(first);    ctx.savechanges(); } 

simple... savechanges throws following exception.

the delete statement conflicted reference constraint fk_entitya_inherits_baseentity". conflict occurred in database "mydb", table "dbo.baseentityset_entitya", column 'id'. statement has been terminated.

how fix this?


update

i noticed difference between how vs2010 , vs2012 model designer outputs sql.

vs2010:

-- creating foreign key on [id] in table 'baseentityset_entitya' alter table [dbo].[baseentityset_entitya] add constraint [fk_entitya_inherits_baseentity]     foreign key ([id])     references [dbo].[baseentityset]         ([id])     on delete no action on update no action; go  -- creating foreign key on [id] in table 'baseentityset_entityb' alter table [dbo].[baseentityset_entityb] add constraint [fk_entityb_inherits_baseentity]     foreign key ([id])     references [dbo].[baseentityset]         ([id])     on delete no action on update no action; go 

notice on delete no action

vs2012

-- creating foreign key on [id] in table 'baseentityset_entitya' alter table [baseentityset_entitya] add constraint [fk_entitya_inherits_baseentity]     foreign key ([id])     references [baseentityset]         ([id])     on delete cascade on update no action; go  -- creating foreign key on [id] in table 'baseentityset_entityb' alter table [baseentityset_entityb] add constraint [fk_entityb_inherits_baseentity]     foreign key ([id])     references [baseentityset]         ([id])     on delete cascade on update no action; go 

notice on delete cascade

that's rather significant...

there appears bug in visual studio 2010 ado entity designer causes incorrect sql generated.

vs2010:

-- creating foreign key on [id] in table 'baseentityset_entitya' alter table [dbo].[baseentityset_entitya] add constraint [fk_entitya_inherits_baseentity]     foreign key ([id])     references [dbo].[baseentityset]         ([id])     on delete no action on update no action; go  -- creating foreign key on [id] in table 'baseentityset_entityb' alter table [dbo].[baseentityset_entityb] add constraint [fk_entityb_inherits_baseentity]     foreign key ([id])     references [dbo].[baseentityset]         ([id])     on delete no action on update no action; go 

notice on delete no action

vs2012

-- creating foreign key on [id] in table 'baseentityset_entitya' alter table [baseentityset_entitya] add constraint [fk_entitya_inherits_baseentity]     foreign key ([id])     references [baseentityset]         ([id])     on delete cascade on update no action; go  -- creating foreign key on [id] in table 'baseentityset_entityb' alter table [baseentityset_entityb] add constraint [fk_entityb_inherits_baseentity]     foreign key ([id])     references [baseentityset]         ([id])     on delete cascade on update no action; go 

notice on delete cascade

after applying these changes database delete without problems.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -