【问题标题】:Code First Migrations in SQL Azure - Tables without a clustered index are not supportedSQL Azure 中的代码优先迁移 - 不支持没有聚集索引的表
【发布时间】:2013-02-27 14:17:48
【问题描述】:

我似乎无法通过代码优先迁移来创建我的 SQL Azure 数据库。

它一直在抱怨 SQL Azure 不支持没有聚集索引的表,我找不到创建数据库的方法。

注意:我在第一次创建数据库时使用CreateDatabaseIfNotExists 创建更改跟踪表,因为显然DropCreateDatabaseIfModelChanges doesn't do that for you

    public partial class IUnityDbContext : DbContext
    {
        public IUnityDbContext()
            : base("Name=IUnityDbContext")
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<IUnityDbContext>()); 
            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<IUnityDbContext>());
        }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserMap());

            base.OnModelCreating(modelBuilder);
        }
    }




    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Users",
                c => new {
                        ... 
                     }
            ).PrimaryKey(u => u.UserId, clustered: true);            
        }

        public override void Down()
        {
            DropTable("dbo.Users");
        }
    }

如果我尝试 `Update-Database 我会得到 ​​p>

 Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.

数据库未创建。

更新:我从头开始并按照this guide 启用自动迁移(刮掉数据库并从不存在的数据库开始,因此我不必删除向上/向下代码从初始迁移)

这次我的数据库已成功创建(之前没有做到这一点),但没有创建表,我仍然得到与以前相同的错误,即不支持没有聚集索引的表。

请指教

【问题讨论】:

  • 所有代码看起来都正确,为什么没有人回答?

标签: sql entity-framework azure ef-code-first azure-sql-database


【解决方案1】:

原来是 Entity Framework 6 -Alpha 3 中的一个错误。我想我应该提到这一点。

https://stackoverflow.com/a/15282861/1267778

【讨论】:

    【解决方案2】:

    这个看起来很相似(虽然它不是理想的解决方案恕我直言):Entity Framework Many-to-Many Clustered vs. Nonclustered Index

    【讨论】:

    • 谢谢,但我不确定它是如何应用的。我使用的是 Code-First 而不是 Model-First,并且迁移甚至无法创建数据库,所以我不知道应该在哪一步更改表。
    • 此外,我的迁移有使主键聚集的行
    • 更新了我的问题。我使用自动迁移从头开始,能够创建数据库但不能创建表(得到相同的错误)。我在 DbContext 构造函数中设置了 MigrateDatabaseToLatestVersion 初始化程序。我可以在哪个步骤(以及如何)注入 sql 语句来解决问题?
    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多