Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create more than one clustered index on table

I am having the following error after typing update-database:

Cannot create more than one clustered index on table 'dbo.AppUsers'. Drop the existing clustered index 'PK_dbo.AppUsers' before creating another.

I am working on an Azure mobile service.

I have three data models:

public class AppUser : EntityData
{
    public string Username { get; set; }
    public virtual ICollection<RatingItem> userRatings { get; set; }
}

public class PlaceItem : EntityData
{
    public string PlaceName { get; set; }
    public int Group { get; set; }
    public string XCoordinate { get; set; }
    public string YCoordinate { get; set; }
}

public class RatingItem : EntityData
{
    public int Ratings { get; set; }
    public string PlaceId { get; set; }
    public AppUser user { get; set; }
}

It has to do with migration because :

  • The initial create is in the _MigrationHistory table, but isn't in the migration folder in the solution explorer.
  • When I add-migration AddAll, I don't get any errors, and AddAll appears in the migration folder, but not in the table.

In the context file:

public class ICbackendContext : DbContext
{
        public DbSet<AppUser> AppUsers { get; set; }
        public DbSet<PlaceItem> PlaceItems { get; set; }
        public DbSet<RatingItem> RatingItems { get; set; }

}

like image 932
Elie Saad Avatar asked Mar 24 '16 19:03

Elie Saad


People also ask

Can I create more than one clustered index?

These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be stored in only one order. For example this will fail: Cannot create more than one clustered index on table 'dbo.Thing'. Drop the existing clustered index 'IX1' before creating another.

How do I create a clustered index in Azure SQL Server?

Applies to: SQL Server (all supported versions) Azure SQL Database You can create clustered indexes on tables by using SQL Server Management Studio or Transact-SQL. With few exceptions, every table should have a clustered index.

What happens to nonclustered indexes when clustered indexes are dropped?

Similarly, if a clustered index is dropped on a table that has several nonclustered indexes, the nonclustered indexes are all rebuilt as part of the DROP operation. This may take significant time on large tables.

How do I create a clustered index using Transact-SQL?

Using Transact-SQL. To create a clustered index. In Object Explorer, connect to an instance of Database Engine. On the Standard bar, click New Query. Copy and paste the following example into the query window and click Execute. USE AdventureWorks2012; GO -- Create a new table with three columns.


1 Answers

Generally, this error message is caused by not running the Mobile Apps/Mobile Services DB generator. Entity Framework does not have an annotation for creating a clustered index that is not a primary key, so the mobile server SDK manually creates the right SQL statements to set CreatedAt as a non-primary key clustered index.

To resolve this, run the database generator before migrations are applied. In the Migrations\Configuration.cs file, include the following:

public Configuration()
{
   AutomaticMigrationsEnabled = false;
   SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
}

To learn more, see How to make data model changes to a .NET backend mobile service. The topic applies to both Mobile Services and Mobile Apps, though some namespaces are different in Mobile Apps.

like image 91
lindydonna Avatar answered Sep 21 '22 21:09

lindydonna