我的 asp.net MVC 应用程序在删除外键约束时遇到问题。来自阅读How to delete a record with a foreign key constraint?我知道我需要某种形式的代码来处理删除。
我目前有以下内容
public override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Supplier>()
.HasOptional(j => j.Agreement)
.WithMany()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
但是 OnModelCreating
生成错误,它在覆盖 protected 继承成员时无法更改访问修饰符
这是因为我相信我正在使用 ApplicationUser 和 IdentityRole。
我可以解决这个问题吗?我需要在哪里放置我的代码?我最初认为我需要将其放入我的身份模型中,如下所示,但我认为这是错误的。
namespace Mark_MVC.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("Mark_MVC", throwIfV1Schema: false)
{
}
public override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Supplier>()
.HasOptional(j => j.Agreement)
.WithMany()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<Agreement> Agreements { get; set; }
public DbSet<Plan> Plans { get; set; }
public DbSet<TimeSheet> TimeSheets { get; set; }
public DbSet<CustomerPlan> CustomerPlans { get; set; }
}
有人能帮忙吗
非常感谢
最佳答案
OnModelCreating 具有 protected 访问修饰符,当您覆盖它时,您必须保持访问修饰符 protected :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//your code here
}
关于c# - ASP.Net MVC 删除带有 FK 约束的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36371899/