How do you ensure Cascade Delete is enabled on a table relationship in EF Code first?

Possible reason why you don’t get cascading delete is that your relationship is optional. Example: public class Category { public int CategoryId { get; set; } } public class Product { public int ProductId { get; set; } public Category Category { get; set; } } In this model you would get a Product table … Read more

Entity Framework 4.1 InverseProperty Attribute and ForeignKey

It is theoretically correct but SQL server (not Entity framework) doesn’t like it because your model allows single employee to be a member of both First and Second team. If the Team is deleted this will cause multiple delete paths to the same Employee entity. This cannot be used together with cascade deletes which are … Read more

MS SQL “ON DELETE CASCADE” multiple foreign keys pointing to the same table?

You’ll have to implement this as an INSTEAD OF delete trigger on insights, to get it to work. Something like: create trigger T_Insights_D on Insights instead of delete as set nocount on delete from broader_insights_insights where insight_id in (select ID from deleted) or broader_insight_id in (select ID from deleted) delete from Insights where ID in … Read more

Cascading deletes with Entity Framework – Related entities deleted by EF

This is exactly how cascading deletes behaves in EF. Setting Cascade on a relation in EF designer instructs EF to execute DELETE statement for each loaded realated entity. It doesn’t say anything about ON CASCADE DELETE in the database. Setting Cascade deletion when using EF needs two steps: Set Cascade on relation in EF designer. … Read more

Is it possible to delete from multiple tables in the same SQL statement?

Nope, you’d need to run multiple statements. Because you need to delete from two tables, consider creating a temp table of the matching ids: SELECT U.Id INTO #RecordsToDelete FROM Users U JOIN LinkingTable J ON U.Id = J.U_Id … And then delete from each of the tables: DELETE FROM Users WHERE Id IN (SELECT Id … Read more

How to add “on delete cascade” constraints?

I’m pretty sure you can’t simply add on delete cascade to an existing foreign key constraint. You have to drop the constraint first, then add the correct version. In standard SQL, I believe the easiest way to do this is to start a transaction, drop the foreign key, add a foreign key with on delete … Read more

What is the difference between CascadeType.REMOVE and orphanRemoval in JPA?

From here:- Cascading Remove Marking a reference field with CascadeType.REMOVE (or CascadeType.ALL, which includes REMOVE) indicates that remove operations should be cascaded automatically to entity objects that are referenced by that field (multiple entity objects can be referenced by a collection field): @Entity class Employee { : @OneToOne(cascade=CascadeType.REMOVE) private Address address; : } Orphan Removal … Read more

How does JPA orphanRemoval=true differ from the ON DELETE CASCADE DML clause

orphanRemoval has nothing to do with ON DELETE CASCADE. orphanRemoval is an entirely ORM-specific thing. It marks “child” entity to be removed when it’s no longer referenced from the “parent” entity, e.g. when you remove the child entity from the corresponding collection of the parent entity. ON DELETE CASCADE is a database-specific thing, it deletes … Read more

tech