Razor 使用关系级联删除实体框架标识用户
我使用了.NETCore3.1框架标识,并用我自己的类扩展了它。但是当我使用内置的DeletePersonalData.OnPostAsync()时,由于我与其他类的关系,它失败了。我不知道如何删除所有扩展类 错误消息:Razor 使用关系级联删除实体框架标识用户,razor,.net-core,entity-framework-6,asp.net-core-mvc,Razor,.net Core,Entity Framework 6,Asp.net Core Mvc,我使用了.NETCore3.1框架标识,并用我自己的类扩展了它。但是当我使用内置的DeletePersonalData.OnPostAsync()时,由于我与其他类的关系,它失败了。我不知道如何删除所有扩展类 错误消息: public class ApplicationDbContext : IdentityDbContext<IdentityUser> { public ApplicationDbContext(DbContextOptions<
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 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()
{
}
}
SqlException:DELETE语句与引用冲突
约束“FK_工作空间_AspNetUsers_OwnerId”。冲突发生了
在数据库“myupload”的表“dbo.workspace”的列“OwnerId”中。这个
声明已终止。
Microsoft.Data.SqlClient.SqlCommand+c.b__164_0(任务
结果)
扩展标识:
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 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()
{
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
公共DbSet MyFiles{get;set;}
公共数据库集工作空间{get;set;}
公共数据库集工作空间权限{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
//自定义ASP.NET标识模型,并根据需要覆盖默认值。
//例如,可以重命名ASP.NET标识表名称等。
//在调用base.OnModelCreating(builder)后添加自定义项;
builder.Entity()
.HasOne(p=>p.Owner)
.有很多
.OnDelete(DeleteBehavior.Cascade);
builder.Entity()
.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("~/");
}
}
公共异步任务OnPostAsync()
{
var user=await\u userManager.GetUserAsync(用户);
if(user==null)
{
返回NotFound($“无法加载ID为“{{u userManager.GetUserId(user)}”的用户”);
}
RequirePassword=await\u userManager.HasPasswordAsync(用户);
如果(需要密码)
{
如果(!wait_userManager.CheckPasswordAsync(user,Input.Password))
{
AddModelError(string.Empty,“密码不正确”);
返回页();
}
}
var result=await\u userManager.deleteAync(用户);
var userId=wait_userManager.GetUserIdAsync(用户);
如果(!result.successed)
{
抛出新的InvalidOperationException($“删除ID为“{userId}”的用户时发生意外错误”);
}
等待_signInManager.SignOutAsync();
_logger.LogInformation(“ID为“{UserId}”的用户删除了自己。”,UserId);
返回重定向(“~/”);
}
}
其中一个类:
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 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()
{
}
}
公共类工作区
{
//基地
公共Guid工作空间ID{get;set;}
公共字符串名称{get;set;}
//保安
公共虚拟标识用户所有者{get;set;}
公共字符串密码{get;set;}
//档案
公共虚拟ICollection MyFile{get;set;}
//统计数字
公共工作区()
{
}
}
使用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);
builder.Entity()
.HasOne(p=>p.Owner)
.有很多
.OnDelete(DeleteBehavior.Cascade);
builder.Entity()
.HasOne(p=>p.Workspace)
.WithMany(b=>b.MyFile);
使用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);
builder.Entity()
.HasOne(p=>p.Owner)
.有很多
.OnDelete(DeleteBehavior.Cascade);
builder.Entity()
.HasOne(p=>p.Workspace)
.WithMany(b=>b.MyFile);
@GertArnold那么我如何用cascade delete定义它们呢?我先用实体框架和代码。@GertArnold我没有成功。我补充了我认为不会成功的东西。同样的错误message@GertArnold谢谢你的指点。我终于解决了。如果你想让我接受你的答案,请加上一个。@GertArnold那么我如何用级联删除来定义它们呢?我先用实体框架和代码。@GertArnold我没有成功。我补充了我认为不会成功的东西。同样的错误message@GertArnold谢谢你的指点。我终于解决了。如果你想让我接受你的答案,请加上一个。