3

I have an issue on my asp.net MVC application with deleting a foreign Key constraint. From reading How to delete a record with a foreign key constraint? I know that I need to have a some form of code to handle the delete.

What I have currently is the following

   public override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Supplier>()
            .HasOptional(j => j.Agreement)
            .WithMany()
            .WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);
    }

However the OnModelCreating is generating an error that it cannot change access modifiers when overriding protected inherited member This is because I believe I am using ApplicationUser and IdentityRole.

Can I get around this and where abouts do I need to place my code? I initialy thought I would need to place this into my identity model as I have shown below, but think that this is wrong.

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; }
}

}

Please can someone help

Many thanks

Community
  • 1
  • 1
markabarmi
  • 245
  • 1
  • 14

1 Answers1

2

OnModelCreating has protected access modifier, when you override it, you have to keep access modifier protected:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //your code here
}
Nemanja Todorovic
  • 2,521
  • 2
  • 19
  • 30