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 :
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; }
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With