EF核心级联删除

编程入门 行业动态 更新时间:2024-10-23 18:21:25
本文介绍了EF核心级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我有一个带有两个FK的实体.如果我删除一个FK表中的一条记录,则会出现典型的引用"错误.在 docs.microsoft/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 docs.microsoft/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.

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

  • 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核心级联删除

    本文发布于:2023-10-16 05:48:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1496678.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:核心   级联   EF

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!