【问题标题】:ASP.Net MVC Delete record with a FK Constraint带有 FK 约束的 ASP.Net MVC 删除记录
【发布时间】:2016-04-02 10:10:13
【问题描述】:

我的 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 正在生成错误cannot change access modifiers when overriding protected inherited member 这是因为我相信我正在使用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; }
}

}

请有人帮忙

非常感谢

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    OnModelCreating 具有受保护的访问修饰符,当您覆盖它时,您必须保持访问修饰符受保护:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //your code here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-29
      • 2011-02-16
      • 1970-01-01
      • 2018-09-21
      • 2021-10-24
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多