简体   繁体   English

如何允许级联删除具有两个其他复杂对象列表的复杂对象?

[英]How to allow cascade delete for complex object that has two lists of other complex objects?

I'm struggling with trying to make it work. 我正在努力使它工作。

I'd want to have an Dealer who has Two Lists of the same type of Items that are in relation, but I'd want also to be able to cascadly delete them. 我希望有一个Dealer ,该Dealer具有相关的相同类型的Items两个清单,但我也希望能够级联删除它们。

public class Dealer
{
    [Key]
    public Guid Id { get; set; }

    public string Name { get; set; }

    public List<Car> OldCars { get; set; } = new List<Car>();

    public List<Car> NewCars { get; set; } = new List<Car>();
}

public class Car
{
    [Key]
    public Guid Id { get; set; }

    public string Model { get; set; }

    public Dealer Dealer { get; set; }
}

What I've been trying: 我一直在尝试:

Attempt #1 尝试#1

modelBuilder.Entity<Car>()
.HasOne(s => s.Dealer)
.WithMany(s => s.OldCars)
.HasForeignKey(x => x.Dealer)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Car>()
.HasOne(s => s.Dealer)
.WithMany(s => s.NewCars)
.HasForeignKey(x => x.Dealer)
.OnDelete(DeleteBehavior.Cascade);

System.InvalidOperationException: ''Dealer' cannot be used as a property on entity type 'Car' because it is configured as a navigation.' System.InvalidOperationException:不能将“经销商”用作实体类型“汽车”的属性,因为它已配置为导航。

Attempt #2 Meanwhile with: 尝试#2同时:

modelBuilder.Entity<Car>()
.HasOne(s => s.Dealer)
.WithMany(s => s.NewCars)
.HasPrincipalKey(x => x.Id) <--- Principal!
.OnDelete(DeleteBehavior.Cascade);     

It works properly, but shows warning that 它工作正常,但显示警告

warn: Microsoft.EntityFrameworkCore.Model[10605] There are multiple relationships between 'Car' and 'Dealer' without configured foreign key properties causing EF to create shadow properties on 'Car' with names dependent on the discovery order. 警告:Microsoft.EntityFrameworkCore.Model [10605]在没有配置外键属性的情况下,“汽车”和“经销商”之间存在多种关系,导致EF在“汽车”上创建阴影属性,其名称取决于发现顺序。

分贝数据

Unfortunely while attempting to perform Delete (cascade) it throws an exception: 不幸的是,在尝试执行删除(级联)时,它引发了一个异常:

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Cars_Dealers_DealerId1". SqlException:DELETE语句与REFERENCE约束“ FK_Cars_Dealers_DealerId1”冲突。 The conflict occurred in database "testDB", table "dbo.Cars", column 'DealerId1'. 数据库“ testDB”的表“ dbo.Cars”的列“ DealerId1”中发生了冲突。 The statement has been terminated. 该语句已终止。

Attempt #3 尝试#3

When I add Dealer's Id to Car 当我将经销商的ID添加到汽车中时

public class Car
{
    [Key]
    public Guid Id { get; set; }

    public string Model { get; set; }

    public Dealer Dealer { get; set; }

    public Guid DealerID { get; set; }
}

It yells about 大喊

System.Data.SqlClient.SqlException: 'Cannot create, drop, enable, or disable more than one constraint, column, index, or trigger named 'FK_Cars_Dealers_DealerId' in this context. System.Data.SqlClient.SqlException:在这种情况下,无法创建,删除,启用或禁用多个名为“ FK_Cars_Dealers_DealerId”的约束,列,索引或触发器。 Duplicate names are not allowed. 不允许重复的名称。 Column names in each table must be unique. 每个表中的列名必须唯一。 Column name 'DealerId' in table 'Cars' is specified more than once.' 表'Cars'中的列名'DealerId'被多次指定。

#Attempt #3.1 #尝试#3.1

So, after renaming Guid DealerID to eg Guid DealerID123 因此,在将Guid DealerID重命名为例如Guid DealerID123

it yells 大喊

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Cars_Dealers_DealerID123". SqlException:INSERT语句与FOREIGN KEY约束“ FK_Cars_Dealers_DealerID123”发生冲突。 The conflict occurred in database "testDB", table "dbo.Dealers", column 'Id'. 数据库“ testDB”的表“ dbo.Dealers”的列“ Id”中发生了冲突。 The statement has been terminated. 该语句已终止。

Anybody has an idea how to make it work? 任何人都有一个想法如何使其工作?

Thanks to Ivan Stoev recommendation, I managed to resolve it as follow: 感谢Ivan Stoev的建议,我设法解决了以下问题:

public class Dealer
{
   [Key]
   public Guid Id { get; set; }
   public string Name { get; set; }
   [NotMapped]
   public List<Car> OldCars { get; set; } => NewCars.Where(c => c.IsOld == true);
   public List<Car> NewCars { get; set; } = new List<Car>();
}

public class Car
{
   [Key]
   public Guid Id { get; set; }
   public string Model { get; set; }
   public Dealer Dealer { get; set; }
   public bool IsOld { get; set; }
}

In EF6 ModelBuilder I did as follow: 在EF6 ModelBuilder中,我做了如下操作:

modelBuilder.Entity<Dealer>()
.HasMany(d => d.NewCars)
.WithRequired()
.WillCascadeOnDelete(true);

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

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