EF Code First cascade delete and update?

前端 未结 1 1463
南笙
南笙 2020-12-19 03:05

My entities are these:

public class Customer
{
    public Customer()
    {
        Invoices = new List();
        Payments = new List

        
相关标签:
1条回答
  • 2020-12-19 03:44

    Description

    You need to configure the modelBuilder in your context.

    Sample

    public class AccountingContext : DbContext, IDisposable
    {
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Payment> Payments { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
            modelBuilder.Entity<Payment>()
                    .HasRequired(s => s.Customer)
                    .WithMany()
                    .WillCascadeOnDelete(true);
    
            base.OnModelCreating(modelBuilder);
        }
    }
    
    0 讨论(0)
提交回复
热议问题