簡體   English   中英

SqlException:在刪除級聯不工作 ASP.NET

[英]SqlException: On Delete Cascade not working ASP.NET

當我嘗試從 ASP.NETUsers 表中刪除用戶時,我得到 SqlException:

SqlException:DELETE 語句與 REFERENCE 約束“FK_Applications_AspNetUsers_UserID”沖突。 沖突發生在數據庫“JobGuide”、表“dbo.Applications”、列“UserID”中。

出現此問題是因為用戶的 Id 是另一個表中的外鍵,但“在刪除級聯時”不起作用。 有關更多詳細信息,這是我的 model:

我的擴展身份用戶:

public class AppUser : IdentityUser
{
    public string Name { get; set; }
    public string City { get; set; }
    public string RoleName { get; set; }

    public virtual ICollection<Application> Applications { get; set; }
}

應用model(即用戶申請工作時):

public class Application
{
    public int ApplicationID { get; set; }

    public int JobID { get; set; }
    public virtual Job Job { get; set; }
    
    public string UserID { get; set; }
    public virtual AppUser User { get; set; }
}

作業 model:

public class Job
{
    public int JobID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Application> Applications { get; set; }
}

所以到目前為止,我創建了兩個一對多關系,AspNetUser 與 Application 一對多,Job 與 Application 一對多。

這是我的 Fluent API 映射配置:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<Application>()
        .HasKey(i => i.ApplicationID);

    builder.Entity<Application>()
           .HasOne<AppUser>(sc => sc.User)
           .WithMany(s => s.Applications)
           .HasForeignKey(sc => sc.UserID)
           .OnDelete(DeleteBehavior.Cascade);

    builder.Entity<Application>()
           .HasOne<Job>(sc => sc.Job)
           .WithMany(s => s.Applications)
           .HasForeignKey(sc => sc.JobID)
           .OnDelete(DeleteBehavior.Cascade);
}

從 controller 中刪除方法:

    var userInfo = await userManager.FindByIdAsync(user.Id);
    if (userInfo == null) 
    {
        return NotFound();
    }
    _ = await userManager.RemoveFromRoleAsync(userInfo, userInfo.RoleName);
    _ = await userManager.DeleteAsync(userInfo);
    
    int rowsAffected = await db.SaveChangesAsync();

知道為什么這個錯誤沒有消失,Fluent API 好嗎? 或者我需要輸入原始 Sql 來刪除該用戶的應用程序,然后再刪除用戶? 我看過幾乎所有類似的問題,但沒有一個對我有用。

好像是應用表中沒有配置級聯刪除,嘗試使用SSMS檢查一下:

Open the SQL Server Object Explorer (or using Server Explorer), find the SQL Server Database, then right click the Applications table -> Script As -> CREATE To -> New Query Window , then check whether the table is configured Cascade delete, check這個截圖:

在此處輸入圖像描述

為了解決這個問題,在使用 Fluent API 映射配置級聯刪除后,請記住啟用遷移並更新數據庫:

  Add-Migration AddCascadeDelete
  Update-Database

此外,您還可以通過執行以下 SQL 命令(通過 SSMS)來配置級聯刪除:

ALTER TABLE [dbo].[Applications]
    ADD CONSTRAINT [FK_Applications_AspNetUsers_UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE;

你能試着反過來設置嗎?

builder.Entity<User>()
    .HasMany<Application>(u => u.Applications)
    .WillCascadeOnDelete();

或在您的代碼上使用.WillCascadeOnDelete()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM