Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Migrations in SQL Azure - Tables without a clustered index are not supported

I can't seem to get my Code-First Migration to create my SQL Azure database.

It keeps complaining about SQL Azure's lack of support for tables without clustered indexes and I cant find a way around to create my database.

Note: I'm using CreateDatabaseIfNotExists to create the change tracking tables on the first time database creation because apparently 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");
        }
    }

If I try to `Update-Database I get

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

The database is not created.

UPDATE: I started from scratch and followed this guide to enable Automatic Migrations (scratched the database and started with a non-existing one so I didn't have to remove the Up/Down code from the Initial migration)

This time my database was successfully created (Did not get this far before) but the tables are not created and I still get the same error as before about no support for tables without clustered indexes.

Please advise

like image 787
parliament Avatar asked Mar 12 '13 06:03

parliament


People also ask

Can we create non clustered index without clustered index SQL Server?

Generally, nonclustered indexes are created to improve the performance of frequently used queries not covered by the clustered index or to locate rows in a table without a clustered index (called a heap). You can create multiple nonclustered indexes on a table or indexed view.

Does every table need a clustered index?

Yes, every table should have a clustered index. The clustered index sets the physical order of data in a table. You can compare this to the ordering of music at a store, by bands name and or Yellow pages ordered by a last name.

How do I create a clustered index in Azure SQL?

On the Table Designer menu, click Indexes/Keys. In the Indexes/Keys dialog box, click Add. Select the new index in the Selected Primary/Unique Key or Index text box. In the grid, select Create as Clustered, and choose Yes from the drop-down list to the right of the property.

When a table is created in SQL Server with out any indexes it is stored as what?

A heap is a table without a clustered index. One or more nonclustered indexes can be created on tables stored as a heap. Data is stored in the heap without specifying an order.


2 Answers

This one looks similar (although it's not the ideal solution imho): Entity Framework Many-to-Many Clustered vs. Nonclustered Index

like image 34
maartenba Avatar answered Sep 30 '22 09:09

maartenba


Turns out to be a bug in Entity Framework 6 -Alpha 3. I guess I should've mentioned that.

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

like image 134
parliament Avatar answered Sep 30 '22 07:09

parliament