EF核心级联删除 [英] EF Core Cascade Delete

查看:51
本文介绍了EF核心级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:Visual Studio,Blazor .NET 5,Azure SQL Server

Context: Visual Studio, Blazor .NET 5, Azure SQL Server

我有一个带有两个FK的实体.如果我删除一个FK表中的一条记录,则会出现典型的引用"错误.在 https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete 似乎是说Cascade Delete是默认设置.当我生成新的迁移(添加迁移级联)时,没有

I have an entity with two FK. If I delete a record in one the FK tables I get the typical Referencing error. In https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete it seems to say the Cascade Delete is the default. When I generate a new Migration (add-migration cascade) there is no

.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

...附加到所生成的迁移文档中所讨论实体的FK属性上.

... attached to the FK properties of the entity in question in the Migration documents generated.

  1. 实体类中是否有可以应用于FK属性的属性?

  1. Is there an Attribute that can be applied to the FK property in the entity class?

在代码中还有其他方法吗?

Is there some other way of doing this in code?

如何修改迁移文档以巩固其地位?

How do I modify the migration documentation to entrench th?

推荐答案

这就是我最终所做的..我是在代码中完成的

This is what I ended up doing .. I did it in code

    public async Task DeleteRound(int Id)
   {
       var round = _context.Rounds.Where(e => e.Id==Id).Include(e => e.Activitys).First();
       _context.Rounds.Remove(round);
       await _context.SaveChangesAsync();
   }

我必须在Round Class中添加以下内容:

I had to add the following to the Round Class:

   public IList<Activity> Activitys { get; } = new List<Activity>();

不需要其他代码.无需如上所述填充列表.看来EF正在无缝地进行着.无需注释迁移文件.

No further code required. No need to populate the list as above. It seems that EF is doing it seamlessly. No need to annotate the Migration files.

这篇关于EF核心级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆