我有级联删除的问题。我想尽可能使用它但在某些情况下它不适合我。 我已将 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);
}
}
正如您所看到的,我有一些存档映射,我已禁用Cascade删除。这是设计原因,因为我不想删除项目并删除创建/修改它的用户:)
如果我删除中心并且它有一些收藏集,则删除正常并删除收藏。同样如果我删除用户并且他有一些 UserCenter ,两者都将被删除,但如果我尝试删除公司(它有用户和用户有UserCenters)它失败,我得到一个例外:
exceptionMessage = DELETE语句与REFERENCE约束“FK_dbo.Users_dbo.Companies_CompanyId”冲突。冲突发生在数据库“melanite”,表“dbo.Users”,列'CompanyId'中。 声明已经终止。
我可以看到用户的 CompanyId 字段正在呻吟,所以我改变了这一行:
modelBuilder.Entity<Company>().HasMany(m => m.Members).WithOptional().HasForeignKey(m => m.CompanyId).WillCascadeOnDelete(false);
因为如果删除用户,我不希望它删除用户所属的公司。 但是,另一方面,如果我删除公司,我确实希望它删除用户 ...
有谁知道如何解决我的问题?
答案 0 :(得分:1)
只需将FK配置为在级联上删除:
modelBuilder.Entity<Company>().HasMany(m => m.Members).WithOptional().HasForeignKey(m => m.CompanyId).WillCascadeOnDelete(true);
级联删除始终在一个方向上工作,从引用的表(公司)到引用表(用户),因此删除公司也将删除其用户,但删除用户不会对引用做任何事情公司