Error when implementing Soft Delete in EF (Map was called more than once for type)

205 views Asked by At

i trying to implement soft delete in Entity Framework

I have this code in OnModelCreating

    if (modelBuilder == null)
    {
        throw new ArgumentNullException("modelBuilder");
    }

    modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");

    EntityTypeConfiguration<ApplicationUser> table = modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");

    table.Property((ApplicationUser u) => u.UserName).IsRequired();

    modelBuilder.Entity<ApplicationUser>().HasMany((ApplicationUser u) => u.Roles);
    modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

    modelBuilder.Entity<ApplicationUser>().HasMany((ApplicationUser u) => u.Groups);

    EntityTypeConfiguration<ApplicationUserGroup> usergroup = modelBuilder.Entity<ApplicationUserGroup>().ToTable("ApplicationUserGroups");

    modelBuilder.Entity<Group>().HasMany((Group g) => g.Roles);
    modelBuilder.Entity<ApplicationRoleGroup>().HasKey((ApplicationRoleGroup gr) => new { RoleId = gr.RoleId, GroupId = gr.GroupId }).ToTable("ApplicationRoleGroups");

    EntityTypeConfiguration<Group> groupsConfig = modelBuilder.Entity<Group>().ToTable("Groups");
    groupsConfig.Property((Group r) => r.Name).IsRequired();

    EntityTypeConfiguration<IdentityUserLogin> entityTypeConfiguration =
        modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) => new { UserId = l.UserId, LoginProvider = l.LoginProvider, ProviderKey = l.ProviderKey }).ToTable("AspNetUserLogins");

    entityTypeConfiguration.HasRequired((IdentityUserLogin u) => u.User);
    EntityTypeConfiguration<IdentityUserClaim> table1 = modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
    table1.HasRequired((IdentityUserClaim u) => u.User);

    modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");

    EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 = modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");
    entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();

    modelBuilder.Entity<Group>().Map(m => m.Requires("IsDeleted").HasValue(false)).Ignore(m => m.IsDeleted);

    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

when i try to Migrate that to Database it gives me this error

Map was called more than once for type 'Group' and at least one of the calls didn't specify the target table name.

this error only occurs when i add this Line of code to OnModelCreating

    modelBuilder.Entity<Group>().Map(m => m.Requires("IsDeleted").HasValue(false)).Ignore(m => m.IsDeleted);

Any Solution for that, i need to do that to get only the rows which are (!Is.Deleted)

this scenario will be for most of my Models,

Please Advice,

0

There are 0 answers