Entity Framework: Set Delete Rule with CodeFirst

What you are looking for can be achieved by setting up an optional association between Guest and Language entities: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Guest>() .HasOptional(p => p.PreferredLanguage) .WithMany() .HasForeignKey(p => p.LanguageID); } Unit Test: using (var context = new Context()) { var language = new Language() { LanguageName = “en” }; var guest … Read more

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

Django – Cascade deletion in ManyToManyRelation

I think you are misunderstanding the nature of a ManyToMany relationship. You talk about “the corresponding BlogEntry” being deleted. But the whole point of a ManyToMany is that each BlogEntryRevision has multiple BlogEntries related to it. (And, of course, each BlogEntry has multiple BlogEntryRevisions, but you know that already.) From the names you have used, … 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