无法在 table 上创建多个聚集索引

Cannot create more than one clustered index on table

输入更新数据库后出现以下错误:

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

我正在开发 Azure 移动服务。

我有三个数据模型:

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; }
}

这与迁移有关,因为:

在上下文文件中:

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

}

通常,此错误消息是由于未 运行 移动 Apps/Mobile 服务数据库生成器引起的。 Entity Framework没有创建非主键聚簇索引的注解,所以移动端SDK手动创建正确的SQL语句将CreatedAt设置为非主键聚集索引。

要解决此问题,运行 应用迁移之前的数据库生成器。在 Migrations\Configuration.cs 文件中,包括以下内容:

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

要了解更多信息,请参阅 How to make data model changes to a .NET backend mobile service。该主题适用于 移动服务和移动应用,但移动应用中的某些命名空间不同。

正如@gorillapower 在评论中所说,这段代码也很重要

modelBuilder.Conventions.Add(new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>( "ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));

在你的

里面
protected override void OnModelCreating(DbModelBuilder modelBuilder)

DbContext 配置中 class。不要忘记重新生成迁移。