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 which has a foreign key to the Category table but this key is nullable and there is no cascading delete setup in the database by default.
If you want to have the relationship required then you have two options:
Annotations:
public class Product { public int ProductId { get; set; } [Required] public Category Category { get; set; } }
Fluent API:
modelBuilder.Entity<Product>() .HasRequired(p => p.Category) .WithMany();
In both cases cascading delete will be configured automatically.
If you want to have the relationship optional but WITH cascading delete you need to configure this explicitely:
modelBuilder.Entity<Product>() .HasOptional(p => p.Category) .WithMany() .WillCascadeOnDelete(true);
Edit: In the last code snippet you can also simply write .WillCascadeOnDelete(). This parameterless overload defaults to true for setting up cascading delete.
See more on this in the documentation
OneToManyCascadeDeleteConvention.entity-frameworktag? I was just wondering why this question suddenly disappeared from the questions under my favorite tags. I've added nowentity-framework-4.1andentity-framework-4and...and...and... but it's getting a bit convoluted now to check all those tags. I am not sure if it wouldn't be better to have a hierarchy of gradually specializing tags on a question. I see a risk that less readers will see questions with only very specialized tags which is not in the interest of the questioner..net,entity-framework,entity-framework-4.1,ef-code-first(or onlycode-firstsince I saw that Mortezza proposed this retagging under tag synonyms). But I admit that this might be too complicated, especially for newcomers and the risk is high that tagging can become chaotic if we have so many specialized tags.