Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

c# - EF Code First cascade delete and update?

My entities are these:

public class Customer
{
    public Customer()
    {
        Invoices = new List<Invoice>();
        Payments = new List<Payment>();
    }

    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public IList<Payment> Payments { get; set; }
}

public class Payment
{
    public int ID { get; set; }
    public int CustomerID { get; set; }
    public decimal CreditPrice { get; set; }
    public decimal DebitPrice { get; set; }
    public DateTime PaymentDate { get; set; }

    [ForeignKey("CustomerID")]
    public Customer Customer { get; set; }
}

and this is my context:

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)
                .WillCascadeOnDelete();

        base.OnModelCreating(modelBuilder);
    }
}

i get this error in WillCascadeOnDelete():

Error 1 'System.Data.Entity.ModelConfiguration.Configuration.RequiredNavigationPropertyConfiguration' does not contain a definition for 'WillCascadeOnDelete' and no extension method 'WillCascadeOnDelete' accepting a first argument of type 'System.Data.Entity.ModelConfiguration.Configuration.RequiredNavigationPropertyConfiguration' could be found (are you missing a using directive or an assembly reference?) D:WorkC# ProjectsVisual Studio 2010WindowsWPFNew folderAccounting Without EF Code FirstAccounting - CopyDAL.EF.CodeFirstEntitiesContextAccountingContext.cs 22 22 DAL.EF.CodeFirst

i want to delete payments of the customer cascading (Just if customer getting deleted). how can i achieve this in EF code first?

also i want to use cascade update. please help me in these issues. thanx.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...