不能在一个表上创建多个聚集索引 [英] Cannot create more than one clustered index on table
问题描述
键入update-database后出现以下错误:
I am having the following error after typing update-database:
不能在表dbo.AppUsers上创建多个聚集索引.在创建另一个索引之前,请删除现有的聚集索引'PK_dbo.AppUsers'.
Cannot create more than one clustered index on table 'dbo.AppUsers'. Drop the existing clustered index 'PK_dbo.AppUsers' before creating another.
我正在使用Azure移动服务.
I am working on an Azure mobile service.
我有三个数据模型:
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; }
}
这与迁移有关,因为:
- 初始创建在 _MigrationHistory 表中,但不在解决方案资源管理器的迁移文件夹中.
- 当我添加迁移AddAll时,我没有收到任何错误,并且AddAll出现在迁移文件夹中,但没有出现在表格中.
- 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.
在上下文文件中:
public class ICbackendContext : DbContext
{
public DbSet<AppUser> AppUsers { get; set; }
public DbSet<PlaceItem> PlaceItems { get; set; }
public DbSet<RatingItem> RatingItems { get; set; }
}
推荐答案
通常,此错误消息是由于未运行Mobile Apps/Mobile Services DB生成器引起的.实体框架没有用于创建不是主键的聚簇索引的注释,因此移动服务器SDK手动创建了正确的SQL语句以将CreatedAt
设置为非主键聚簇索引.
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.
要解决此问题,请在应用迁移之前运行数据库生成器.在Migrations\Configuration.cs
文件中,包括以下内容:
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.
这篇关于不能在一个表上创建多个聚集索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!