【问题标题】:Azure mobile services with .NET backend- There is already an object named in the database带有 .NET 后端的 Azure 移动服务 - 数据库中已经存在一个对象
【发布时间】:2017-07-12 15:38:25
【问题描述】:

我按照 Microsoft 的本教程使用 Azure 门户为 Xamarin iOS 应用创建了一个 .NET 后端:tutorial

然后我使用了现有的数据库和这个新的后端。但是,当我尝试登录我的移动应用程序时,登录失败并出现上述错误:

There is already an object named in the database

现有数据库在另一个订阅下,该订阅将被停用。我将其导入新订阅并进行了设置。

我在这里看到了大量类似的问题,也尝试了一些解决方案。我仍然有错误。

由于我在新后端更改了命名空间,因此我从 StackOverflow 上的答案中阅读了此内容:

您的数据库中有一个名为 dbo.__MigrationHistory 的表。该表有一个名为 ContextKey 的列。 此列的值基于您的命名空间。例如是“DataAccess.Migrations.Configuration”。 当您更改命名空间时,会导致具有不同命名空间的重复表名。 因此,在代码端更改命名空间后,也要在数据库中更改此表中的命名空间(适用于所有行)。

我怀疑这可能是我的问题,但我目前无法访问我的数据库。

我也试过这个没有用的解决方案:

Database.SetInitializer<YourContext>(null);

最后,我尝试在 Xamarin Studio 的 Package Console Extension 中使用 update-database 命令,但出现错误 :

command update-databse not found

我现在不确定如何解决此问题。有人能指出我正确的方向吗?

谢谢。

编辑

根据上面关于更改上下文的解释,我已经在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());
    }

  }

那么这会更新dbo.__MigrationHistory 表还是我还需要运行更新命令?

【问题讨论】:

    标签: c# azure xamarin.ios azure-mobile-services


    【解决方案1】:

    我设法通过更改dbo.__MigrationHistory 表中所有行的 contextKey 值来解决这个问题命名空间,它指向 DBContext。所以代替这个:

    testService.Models.testserviceContext 
    

    来自这一行:

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

    我将表中的值更新为:

    testService.Migrations.Configuration
    

    这是我的配置类的新命名空间。

    这解决了问题,我现在可以登录我的数据库了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      相关资源
      最近更新 更多