If your model has a one-to-many relationship, the EF code will first allow cascading deletion by default. This way, you really don't need to do anything, but consider the script you want to override and disable cascading deletion. Here's how to do it using the Fluent API with EF CTP5 earlier:
public class Customer { public int CustomerId { get; set; } public virtual ICollection<Order> Orders { get; set; } } public class Order { public int OrderId { get; set; } public int CustomerId { get; set; } public virtual Customer Customer { get; set; } } public class StackoverflowContext : DbContext { public DbSet<Customer> Customers { get; set; } public DbSet<Order> Orders { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Customer>() .HasMany(c => c.Orders) .WithRequired(o => o.Customer) .HasForeignKey(o => o.CustomerId) .WillCascadeOnDelete(false); } }
Morteza manavi
source share