EF 6 code-first Cannot find the object "dbo.AspNetUserRoles" because it does not exist or you do not have permissions

asp.net-identity c# ef-migrations entity-framework entity-framework-6

Question

I'm trying to migrate tables and data to my existing database using a code-first approach.
When I run the Add-Migration [somename] I end up with the following exception:

Cannot find the object "dbo.AspNetUserRoles" because it does not exist or you do not have permissions.

Now here's the deal, my tables are named like aspnet_Users, aspnet_UserRoles etc. As you can see my generated migration-file capitalizes the table name. I've tried to rename those manually, unfortunately without any progress. I can't seem to figure out what causes this.

My Database entity looks like this:

public partial class MyDBContext : IdentityDbContext<ApplicationUser>
{
    public FunkaDbContext()
        : base("name=MyDBContext", throwIfV1Schema: false)
    {
        Database.SetInitializer<MyDBContext>(new DropCreateDatabaseIfModelChanges<MyDBContext>());
    }

    public virtual DbSet<educationSignup> EducationSignups { get; set; }
    public virtual DbSet<ParticipantsInformation> ParticipantsInformations { get; set; }
    public virtual DbSet<PaymentInformation> PaymentInformations { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<educationSignup>()
            .HasMany(e => e.participantsInformations)
            .WithRequired(e => e.educationSignup)
            .HasForeignKey(e => e.SignupId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<educationSignup>()
            .HasMany(e => e.paymentInformations)
            .WithRequired(e => e.educationSignup)
            .HasForeignKey(e => e.SignupId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<IdentityUser>().HasKey<string>(u => u.Id);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    }

    public static MyDbContext Create()
    {
        return new MyDbContext();
    }
}
1
1
9/7/2015 7:10:13 PM

Popular Answer

Remove migration history in db and use bottom mapping for your context

 base.OnModelCreating(modelBuilder);
 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

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

 modelBuilder.Entity<IdentityUserLogin>()
            .HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
            .ToTable("AspNetUserLogins");
0
10/31/2017 5:37:56 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow