简体   繁体   English

带有.NET后端的Azure移动服务-数据库中已经有一个命名的对象

[英]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 我已经创建了使用以下微软本教程在Azure门户Xamarin iOS应用.NET后端: 教程

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: 由于我在新的后端中更改了名称空间,因此我从此处的StackOverflow答案中读取了此内容:

There is a table in your data base called dbo.__MigrationHistory. 您的数据库中有一个名为dbo .__ MigrationHistory的表。 The table has a column called ContextKey. 该表具有称为ContextKey的列。 The value of this column is based on your namespace. 该列的值基于您的名称空间。 for example is "DataAccess.Migrations.Configuration". 例如“ 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 : 最后,我尝试在Xamarin Studio的Package Console Extension中使用update-database命令,但出现错误:

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: 基于上面有关更改上下文的说明,我已经在Configuration.cs类中对此进行了更新,如下所示:

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? 那么这是否会更新dbo.__MigrationHistory表,还是我仍然需要运行更新命令?

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. 我根据上面Elnaz SO问题答案的解释更改了dbo.__MigrationHistory表中所有行的contextKey值,从而解决了这个问题 。我正在查看错误的名称空间,该名称空间指向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. 这是我的Configuration类的新名称空间。

This resolved the issue and I am now able to log into my database. 这样就解决了问题,现在我可以登录数据库了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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