1

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();
    }
}
dont_trust_me
  • 540
  • 8
  • 24

1 Answers1

0

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");
pejman
  • 740
  • 7
  • 13