When I try to delete a user from the ASP.NETUsers table I get SqlException:
SqlException: The DELETE statement conflicted with the REFERENCE
constraint "FK_Applications_AspNetUsers_UserID". The conflict occurred
in database "JobGuide", table "dbo.Applications", column 'UserID'.
This problem is occurring because the User's Id is the Foreign key in another table, but "On delete cascade" is not working. For more details this is my model:
My extended Identity User:
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; }
}
Application model (i.e. when a user applies for a job):
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; }
}
Job model:
public class Job
{
public int JobID { get; set; }
public string Title { get; set; }
public virtual ICollection<Application> Applications { get; set; }
}
So up to here I created two One to Many relationships, AspNetUser one to many with Application and Job one to many with Application.
And this is my Fluent API mapping configuration:
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);
}
Delete method from 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();
Any idea why this error is not disappearing, is Fluent API good? or i need to type raw Sql to delete the Application with that User once and then the User? I have looked at almost all similar questions but none of them are working for me.
question from:
https://stackoverflow.com/questions/65911623/sqlexception-on-delete-cascade-not-working-asp-net 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…