本文介绍了EF代码首先级联删除和更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的实体是这些:
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; }
}
这是我的上下文:
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);
}
}
我在WillCascadeOnDelete()
i get this error in WillCascadeOnDelete():
我想删除客户级联付款(只要客户被删除) 。我如何才能在EF代码中实现这一点?
i want to delete payments of the customer cascading (Just if customer getting deleted). how can i achieve this in EF code first?
我也想使用级联更新。
请帮我解决这些问题。
thanx。
also i want to use cascade update.please help me in these issues.thanx.
推荐答案
描述
在您的上下文中配置modelBuilder。
Description
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);
}
}
这篇关于EF代码首先级联删除和更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!