0

I am getting this error when I try to register a user etc:

Invalid object name 'dbo.AspNetUsers'.

Which gives me 2 questions:
1) How can I solve this problem
2) Why do I have this problem?! ... there are a few similar questions showing work arounds (hacks?), but nothing I've seem explains why it requires doing

I get the error when I run my website on Azure. If I run in locally (i.e. debug directly from VS IIS) on my machine it runs correctly. The locally run website is still accessing the same Azure SQL database without issue and with the same connection string:

<add name="DefaultConnection" connectionString="data source=tcp:mysqlserver.database.windows.net,1433;initial catalog=mysqldb;user id=mysqluser;password=mypass;multipleactiveresultsets=True;connect timeout=30;encrypt=True;trustservercertificate=False;application name=EntityFramework" providerName="System.Data.SqlClient" />

It can access the other tables in the database fine.

I've tried adding the user my to the databases db_owner role.

I've tried using the sql-user from my connection string, the database name in th:

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

    // Also tried this
    //modelBuilder.HasDefaultSchema("mysqluser");

    string schema = "mysqluser";
    modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles", schema);
    modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims", schema);
    modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins", schema);
    modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles", schema);
    modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers", schema);
}

This is mainly based on this question: ASP.Net Identity - Use custom Schema ...I haven't done the convoluted answer yet - creating a migration, edit it, apply it, delete it, apply it again - surely that's a hack around something that should work "out of the box".

Additional information: I have just re-created this database using migration so that the AspNet tables would be recreated along with my own.

Community
  • 1
  • 1
noelicus
  • 14,468
  • 3
  • 92
  • 111

3 Answers3

2

OK. So the red-herring here is that other people have posted the same error such as these questions: - ASP.Net Identity - Use custom Schema
- Invalid object name 'dbo.AspNetUsers' in Asp.NET MVC 5 Entity Framework
- Invalid object name 'dbo.AspNetUsers'

But for me after trying all of the above I have tracked the issue to the Publish dialog in visual studio updating my connection string for me (to look at an old database). I found this after debugging what the connection string was on my Azure published website.

Community
  • 1
  • 1
noelicus
  • 14,468
  • 3
  • 92
  • 111
  • Hi, actually I have exactly the same issue. You just updated connection string? That is it? You took the new one from Azure database details? –  Aug 21 '18 at 19:48
  • Well, my connection string was correct in the Web.config files, but the publish settings were set to do their own overriding and reset it to something else as it published (to azure). That was it! – noelicus Aug 22 '18 at 06:45
  • Thanks for feedback. I solved my issue yesterday with using DB query on the Azure server for Identity DB. I just was not able to migrate Identity DB to Azure. –  Aug 22 '18 at 08:36
  • Glad you're sorted. – noelicus Aug 22 '18 at 11:32
  • Thank god i decided to google, this would've been a 5 hour trip – Renet Aug 26 '19 at 00:35
0

I didn't migrate the database properly causing the same error when doing a POST request.

xtds
  • 2,453
  • 2
  • 19
  • 12
0

if your application running good locally(iis) while throwing exception in azure, then you need to turn off application insights of your resource in azure.