更新数据库密钥,聚簇索引错误

时间:2015-10-22 11:09:24

标签: asp.net sql-server azure visual-studio-2013

我在一个asp.net web api项目上使用VS2013和一个部署在Azure上的实体框架

我试图改变其中一个表上的键,而不是由2个由3个键组成的外键组成。使用Add-Migration命令生成以下迁移

public partial class ChangeKeys_v4 : DbMigration
{
    public override void Up()
    {
        DropPrimaryKey("dbo.MyTable");
        AddPrimaryKey("dbo.MyTable", new[] { "ClientId", "Order", "ZoneId" });
    }

    public override void Down()
    {

        DropPrimaryKey("dbo.MyTable");
        AddPrimaryKey("dbo.MyTable", new[] { "ClientId", "ZoneId" });
    }
}

这是更改为包含订单作为键的实体类:

public class MyTable
{
    [Key, Column(Order = 1), ForeignKey("Client")]
    public int ClientId { get; set; }
    [Key, Column(Order = 2)]
    public int Order { get; set; }
    [Key, Column(Order = 3), ForeignKey("Zone")]
    public string ZoneId { get; set; }

    public virtual Client Client { get; set; }
    public virtual Zone Zone { get; set; }

    public MyTable() { }
    public MyTable(Client c, int o, Zone z)
    {
        Client = c;
        Order = o;
        Zone = z;
    }
}

我已经在我的开发环境中成功Update-Database了,但是在文本环境中这样做时出现以下错误

Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.
Could not drop constraint. See previous errors.
The statement has been terminated.

我可以对我的migation类进行哪些更改以使其正常工作。

我已经看到了一些解决方案,并且大多数人都说要放弃桌面,而且当我必须推向生产环境时,这不是一个真正的选择。

1 个答案:

答案 0 :(得分:1)

对于托管数据库的Azure,您无法为此更改执行EF代码 - >数据库升级。

首先,你必须要有停机时间。然后在SSMS中运行连接到Azure DB的脚本。该脚本将:

  1. 将MyTable重命名为例如TableOld
  2. 使用新PK,FK等创建MyTable
  3. 插入MyTable(列名)从MyTableOld中选择(列名);
  4. 运行一些检查:两个表中的行计数,以及您知道结果的其他查询。基本上检查一切正常。
  5. 删除表MyTableOld
  6. 也许会延迟第5步,直到用户确定它已经完成测试后才能确定它。