级联删除 Entity Framework 具有关系的身份用户
Cascade delete Entity Framework Identity User with relations
我使用了 .net Core 3.1 脚手架标识,并用我自己的 类 对其进行了扩展。但是当我使用 DeletePersonalData.OnPostAsync() 中的构建时,由于我与其他 类 的关系,它失败了。我不明白如何进行删除以删除所有扩展 类。
错误信息:
SqlException: The DELETE statement conflicted with the REFERENCE
constraint "FK_Workspaces_AspNetUsers_OwnerId". The conflict occurred
in database "myupload", table "dbo.Workspaces", column 'OwnerId'. The
statement has been terminated.
Microsoft.Data.SqlClient.SqlCommand+<>c.b__164_0(Task
result)
扩展身份:
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<MyFile> MyFiles { get; set; }
public DbSet<Workspace> Workspaces { get; set; }
public DbSet<WorkspacePermission> WorkspacePermissions { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<Workspace>()
.HasOne(p => p.Owner)
.WithMany()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<MyFile>()
.HasOne(p => p.Workspace)
.WithMany(b => b.MyFile);
}
}
以及删除方法:
public async Task<IActionResult> OnPostAsync()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
RequirePassword = await _userManager.HasPasswordAsync(user);
if (RequirePassword)
{
if (!await _userManager.CheckPasswordAsync(user, Input.Password))
{
ModelState.AddModelError(string.Empty, "Incorrect password.");
return Page();
}
}
var result = await _userManager.DeleteAsync(user);
var userId = await _userManager.GetUserIdAsync(user);
if (!result.Succeeded)
{
throw new InvalidOperationException($"Unexpected error occurred deleting user with ID '{userId}'.");
}
await _signInManager.SignOutAsync();
_logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);
return Redirect("~/");
}
}
类之一:
public class Workspace
{
// Base
public Guid WorkspaceID { get; set; }
public string Name { get; set; }
// Security
public virtual IdentityUser Owner { get; set; }
public string Password { get; set; }
// Files
public virtual ICollection<MyFile> MyFile { get; set; }
// Statistics
public Workspace()
{
}
}
使用 Fluent API 我设法在 OnModelCreating 方法中使用此代码将级联删除添加到 IdentityUser。
builder.Entity<Workspace>()
.HasOne(p => p.Owner)
.WithMany()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<MyFile>()
.HasOne(p => p.Workspace)
.WithMany(b => b.MyFile);
我使用了 .net Core 3.1 脚手架标识,并用我自己的 类 对其进行了扩展。但是当我使用 DeletePersonalData.OnPostAsync() 中的构建时,由于我与其他 类 的关系,它失败了。我不明白如何进行删除以删除所有扩展 类。
错误信息:
SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Workspaces_AspNetUsers_OwnerId". The conflict occurred in database "myupload", table "dbo.Workspaces", column 'OwnerId'. The statement has been terminated. Microsoft.Data.SqlClient.SqlCommand+<>c.b__164_0(Task result)
扩展身份:
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<MyFile> MyFiles { get; set; }
public DbSet<Workspace> Workspaces { get; set; }
public DbSet<WorkspacePermission> WorkspacePermissions { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<Workspace>()
.HasOne(p => p.Owner)
.WithMany()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<MyFile>()
.HasOne(p => p.Workspace)
.WithMany(b => b.MyFile);
}
}
以及删除方法:
public async Task<IActionResult> OnPostAsync()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
RequirePassword = await _userManager.HasPasswordAsync(user);
if (RequirePassword)
{
if (!await _userManager.CheckPasswordAsync(user, Input.Password))
{
ModelState.AddModelError(string.Empty, "Incorrect password.");
return Page();
}
}
var result = await _userManager.DeleteAsync(user);
var userId = await _userManager.GetUserIdAsync(user);
if (!result.Succeeded)
{
throw new InvalidOperationException($"Unexpected error occurred deleting user with ID '{userId}'.");
}
await _signInManager.SignOutAsync();
_logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);
return Redirect("~/");
}
}
类之一:
public class Workspace
{
// Base
public Guid WorkspaceID { get; set; }
public string Name { get; set; }
// Security
public virtual IdentityUser Owner { get; set; }
public string Password { get; set; }
// Files
public virtual ICollection<MyFile> MyFile { get; set; }
// Statistics
public Workspace()
{
}
}
使用 Fluent API 我设法在 OnModelCreating 方法中使用此代码将级联删除添加到 IdentityUser。
builder.Entity<Workspace>()
.HasOne(p => p.Owner)
.WithMany()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<MyFile>()
.HasOne(p => p.Workspace)
.WithMany(b => b.MyFile);