【问题标题】:EF Core Code-First Migration Cascade delete problemEF Core Code-First Migration Cascade删除问题
【发布时间】:2020-04-29 09:42:19
【问题描述】:

我已经开始为一个项目处理 后端 部分,目前,我正在尝试使用 EF Core 代码生成数据库 -首先,但我在尝试运行 迁移时遇到了问题(与cascade delete )。

首先,我将在 ERD 下方附上我在 C# 中创建的 DB 和

而对应的分类是:

Account.cs

public class Account
{
   public int Id { get; set; }
   public string Email { get; set; }
   public string ExternalAccountId { get; set; }

   public int CustomerId { get; set; }
   public Customer Customer { get; set; }
}

BillingAddress.cs

public class BillingAddress
{
   public int Id { get; set; }
   public string Address { get; set; }

   public int CustomerId { get; set; }
   public Customer Customer { get; set; }

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

BillingDetail.cs

public class BillingDetail
{
   public int Id { get; set; }
   public int CreditCardNumber { get; set; }
   public DateTime ExpirationDate { get; set; }
   public short Cvv { get; set; }

   public int CustomerId { get; set; }
   public Customer Customer { get; set; }

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

Customer.cs

public class Customer
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string PhoneNumber { get; set; }
   public int Cnp { get; set; }

   public Account Account { get; set; }

   public List<BillingDetail> BillingDetails { get; set; }

   public List<BillingAddress> BillingAddresses { get; set; }

   public List<Rental> Rentals { get; set; }
}

Payment.cs

public class Payment
{
    public int Id { get; set; }

    public int RentalId { get; set; }
    public Rental Rental { get; set; }

    public int BillingDetailId { get; set; }
    public BillingDetail BillingDetail { get; set; }

    public int BillingAddressId { get; set; }
    public BillingAddress BillingAddress { get; set; }
}

Rental.cs

public class Rental
{
   public int Id { get; set; }
   public DateTime PickupDate { get; set; }
   public DateTime DropoffDate { get; set; }

   public int CustomerId { get; set; }
   public Customer Customer { get; set; }

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

   public List<RentalVehicle> RentalVehicles { get; set; }
}

RentalVehicle.cs

public class RentalVehicle
{
   public int Id { get; set; }

   public int RentalId { get; set; }
   public Rental Rental { get; set; }

   public int VehicleId { get; set; }
   public Vehicle Vehicle { get; set; }
}

Vehicle.cs

public class Vehicle
{
   public int Id { get; set; }
   public string Model { get; set; }
   public string Brand { get; set; }
   public string Color { get; set; }
   public short ManufactureYear { get; set; }
   public string Vin { get; set; }
   public int Mileage { get; set; }
   public short Horsepower { get; set; }
   public string GearBox { get; set; }
   public byte EngineSize { get; set; }
   public float Price { get; set; }
   public byte NoDoors { get; set; }

   public List<RentalVehicle> RentalVehicles { get; set; }
}

我已生成迁移,当我尝试运行它时,我收到如下错误:

Introducing FOREIGN KEY constraint 'FK_Payments_BillingDetails_BillingDetailId' on table 'Payments' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

我找到了一些解决方法,可以在迁移中将 ReferentialAction.Cascade 替换为 ReferentialAction.NoAction 用于 onDelete,但我不确定如果我真的应该这样做,以及从长远来看它将如何影响我的应用程序。

我应该怎么做才能解决这个问题?最佳做法是什么?

【问题讨论】:

    标签: c# database entity-framework-core ef-code-first


    【解决方案1】:

    如果您正在编辑迁移,您可能需要更改您的实体配置。

    例子:

    public class PaymentsConfiguration : IEntityTypeConfiguration<Payments>
        {
            public override void Configure(EntityTypeBuilder<Payments> builder)
            {
                builder.HasOne(x => x.BillingDetail).WithMany().OnDelete(DeleteBehavior.Restrict);
                builder.HasOne(x => x. BillingAddress).WithMany().OnDelete(DeleteBehavior.Restrict); //maybe you need to do this too
                builder.HasOne(x => x. Rental).WithMany().OnDelete(DeleteBehavior.Restrict); //maybe you need to do this too
            }
        }
    

    我会查看以下有关配置的文档/教程:

    在我过去做过的项目中,一些主要开发人员建议我不要级联删除。尽管它很方便,但您应该注意要删除的数据...... EF Core 很棒,但有时它会让您默认执行一些 DBA 不喜欢的行为。

    【讨论】:

    • 正确,应该更改映射,而不是迁移代码。但永远不要说永远。级联删除很棒,DBA 喜欢它。
    • @GertArnold,我同意,正如我提到的首席开发人员相信从不级联删除...我相信这是他的个人喜好,而不是我的...我有时对设计感到困惑级联删除与何时不这样做。 EF 很棒,但我的意思是,有时我希望我们有真正的 DBA 可以向其提出想法。我觉得大多数公司(或开发团队)没有 DBA(随时可用),让开发人员整天 EF,这可能会产生好坏参半的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多