简体   繁体   中英

Azure mobile services with .NET backend- There is already an object named in the database

I have created a .NET backend for a Xamarin iOS app using the Azure portal following this tutorial from Microsoft : tutorial

I then used an existing database, with this new backend. When I try to log into my mobile app however, the log in fails with the above error:

There is already an object named in the database

The existing database was under a different subscription which will be deactivated. I imported it into the new subscription and set it up.

I have seen tons of similar questions here and have tried some solutions too. I still have the error.

Since I changed the namespace in the new backend,I have read this from an answer here on StackOverflow:

There is a table in your data base called dbo.__MigrationHistory. The table has a column called ContextKey. The value of this column is based on your namespace. for example is "DataAccess.Migrations.Configuration". When you change the namespace, it causes duplicate table names with different namespaces. So, after you change namespace in code side, change the namespace in this table in database, too, (for all rows).

I suspect that this might be an issue in my case, but I am currently unable to access my database.

I have also tried this solution which hasn't worked :

Database.SetInitializer<YourContext>(null);

And lastly, I have tried to use the update-database command in the Package Console Extension i Xamarin Studio but I get the error :

command update-databse not found

I'm now not sure how to resolve this issue. Could someone point me in the right direction?

Thanks.

EDIT

Based on the explanation above regarding changing the context,I already updated this in the Configuration.cs class as below:

internal sealed class Configuration : DbMigrationsConfiguration<testService.Models.testserviceContext>  // new namespace changed here
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;

        SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
    }

  }

So does this update the dbo.__MigrationHistory table or do I still need to run an update command?

I managed to resolve this by changing the contextKey values for all rows in the dbo.__MigrationHistory table based on the explanation above from the answer here by Elnaz SO question .I was looking at the wrong namespace, which was pointing to the DBContext. So instead of this :

testService.Models.testserviceContext 

which is from this line:

internal sealed class Configuration : DbMigrationsConfiguration<testService.Models.testserviceContext>  // new namespace changed here

I updated the values in the table to this:

testService.Migrations.Configuration

which is the new namespace of my Configuration class.

This resolved the issue and I am now able to log into my database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM