gpt4 book ai didi

c# - EntityFramework 和级联删除

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:52 26 4
gpt4 key购买 nike

我对级联删除有疑问。如果可能的话,我想使用它,但在某些情况下它对我不起作用。我已将我的 DbContext 设置为如下所示:

public class DatabaseContext : IdentityDbContext<User>
{

// Define our tables
public DbSet<Company> Companies { get; set; }
public DbSet<Center> Centers { get; set; }
public DbSet<Collection> Collections { get; set; }
public DbSet<UserCenter> UserCenters { get; set; }
public DbSet<Journal> Journal { get; set; }

/// <summary>
/// static constructor (only gets called once)
/// </summary>
static DatabaseContext()
{

// Create the database and insert our records
Database.SetInitializer<DatabaseContext>(new DatabaseInitializer());
}

/// <summary>
/// Default constructor
/// </summary>
public DatabaseContext()
: base("DefaultConnection")
{

// Write our SQL to the debug window
this.Database.Log = s => Debug.WriteLine(s);

// Disable Lazy Loading
base.Configuration.LazyLoadingEnabled = false;
}

// Public static method for OWIN
public static DatabaseContext Create()
{
return new DatabaseContext();
}

/// <summary>
/// Overrides the inherited OnModelCreated method.
/// </summary>
/// <param name="modelBuilder">The DbModelBuilder</param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

// Change default table names for Identity Framework
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");

// Create mappings for Identity Framework
modelBuilder.Entity<IdentityUserLogin>().HasKey(m => new { m.UserId, m.ProviderKey });
modelBuilder.Entity<IdentityRole>().HasKey<string>(m => m.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(m => new { m.RoleId, m.UserId });
modelBuilder.Entity<User>().HasMany(m => m.Logins).WithRequired().HasForeignKey(m => m.UserId);
modelBuilder.Entity<IdentityRole>().HasMany(m => m.Users).WithRequired().HasForeignKey(m => m.RoleId);

// Create our mappings
modelBuilder.Entity<UserCenter>().HasKey(m => new { m.UserId, m.CenterId });
modelBuilder.Entity<Center>().HasRequired(m => m.Company).WithMany(m => m.Centers).HasForeignKey(m => m.CompanyId);
modelBuilder.Entity<Center>().HasMany(m => m.Collections).WithRequired().HasForeignKey(m => m.CenterId);
modelBuilder.Entity<Company>().HasMany(m => m.Members).WithOptional().HasForeignKey(m => m.CompanyId).WillCascadeOnDelete(false);

// Archive relationships
modelBuilder.Entity<User>().HasOptional(m => m.CreatedBy).WithMany().HasForeignKey(m => m.CreatedById).WillCascadeOnDelete(false);
modelBuilder.Entity<User>().HasOptional(m => m.ModifiedBy).WithMany().HasForeignKey(m => m.ModifiedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Company>().HasRequired(m => m.CreatedBy).WithMany().HasForeignKey(m => m.CreatedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Company>().HasRequired(m => m.ModifiedBy).WithMany().HasForeignKey(m => m.ModifiedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Collection>().HasRequired(m => m.CreatedBy).WithMany().HasForeignKey(m => m.CreatedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Collection>().HasRequired(m => m.ModifiedBy).WithMany().HasForeignKey(m => m.ModifiedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Center>().HasRequired(m => m.CreatedBy).WithMany().HasForeignKey(m => m.CreatedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Center>().HasRequired(m => m.ModifiedBy).WithMany().HasForeignKey(m => m.ModifiedById).WillCascadeOnDelete(false);
modelBuilder.Entity<Journal>().HasRequired(m => m.User).WithMany().HasForeignKey(m => m.UserId).WillCascadeOnDelete(false);
}
}

如您所见,我有一些存档映射,我已从中禁用级联删除。这是设计使然,因为我不想删除一个项目,它会删除创建/修改它的用户:)

如果我删除一个 Center 并且它有一些 Collections,它会正常删除并且删除这些集合。如果我删除一个 User 并且他有一些 UserCenter 也会被删除,但是如果我尝试删除一个 Company (它有用户,用户有用户中心)它失败了,我得到一个异常:

exceptionMessage=The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Users_dbo.Companies_CompanyId". The conflict occurred in database "melanite", table "dbo.Users", column 'CompanyId'. The statement has been terminated.

我可以看到它在 UserCompanyId 字段中提示,所以我更改了这一行:

modelBuilder.Entity<Company>().HasMany(m => m.Members).WithOptional().HasForeignKey(m => m.CompanyId).WillCascadeOnDelete(false);

因为如果我删除一个用户,我不希望它删除用户所属的公司。但是,另一方面,如果我删除一个 Company 我确实希望它删除 Users...

有人知道如何解决我的问题吗?

最佳答案

只需配置您的 FK 以在级联中删除:

modelBuilder.Entity<Company>().HasMany(m => m.Members).WithOptional().HasForeignKey(m => m.CompanyId).WillCascadeOnDelete(true);

级联删除总是在一个方向上进行,从引用表(公司)到引用表(用户),因此删除公司也会删除其用户,但删除用户不会对引用的公司做任何事情。

关于c# - EntityFramework 和级联删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31460080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com