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:\Work\C# Projects\Visual Studio 2010\Windows\WPF\New folder\Accounting Without EF Code First\Accounting - Copy\DAL.EF.CodeFirst\Entities\Context\AccountingContext.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.
CASCADE. It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is either deleted or updated when the parent data is deleted or updated.
Cascade delete in the database Many database systems also offer cascading behaviors that are triggered when an entity is deleted in the database. EF Core configures these behaviors based on the cascade delete behavior in the EF Core model when a database is created using EnsureCreated or EF Core migrations.
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.
You need to configure the modelBuilder in your 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)
.WithMany()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With