简体   繁体   English

EF Core Code-First Migration 级联删除问题

[英]EF Core Code-First Migration Cascade delete problem

I've started to work on the back-end part for a project and currently, I'm trying to generate the DB using EF Core Code-First but I've encountered an issue when I tried to run the migration (is related to cascade delete ).我已经开始处理一个项目的后端部分,目前,我正在尝试使用EF Core Code-First生成数据库,但是当我尝试运行迁移时遇到了一个问题(与级联删除)。

First of all, I'll attach below the ERD for the DB and the classes I've created in C#:首先,我将在数据库和我在 C# 中创建的ERD下方附加:

在此处输入图片说明

And the corresponding classed are:对应的分类是:

Account.cs账户.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 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 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客户.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 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出租.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 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车辆.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; }
}

I've generated the migration and when I try to run it, I'm getting errors like this:我已经生成了迁移,当我尝试运行它时,我收到如下错误:

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.

I've found some fixed to replace ReferentialAction.Cascade with ReferentialAction.NoAction for onDelete in migration, but I'm not sure if I really should do this and how it will impact this thing my app in the long term.我已经发现了一些固定的迁移ReferentialAction.NoActiononDelete更换ReferentialAction.Cascade,但我不知道如果我真的应该这样做,以及它如何将在长期影响这个东西我的应用程序。

What exactly should I do to fix this and which is the best practice?我到底应该怎么做才能解决这个问题,这是最佳实践?

If you are editing a migration, chances are you need to make a change in your configuration of your entity.如果您正在编辑迁移,您可能需要更改实体的配置。

Example:例子:

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

I would review the following documentation / tutorials regarding configurations:我会查看以下有关配置的文档/教程:

In projects I've done in the past and some lead developers have suggested to me to never cascade deletes.在我过去完成的项目中,一些主要开发人员向我建议不要级联删除。 As convenient as it is, you should be mindful as to what data you are deleting... EF Core is great, but sometimes it defaults you to some behaviors a DBA will not be happy about.尽管它很方便,但您应该注意要删除的数据……EF Core 很棒,但有时它会将您默认为某些 DBA 不会满意的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM