无法在表上创建多个聚簇索引

键入update-database后出现以下错误:

无法在表’dbo.AppUsers’上创建多个聚簇索引。 删除现有的聚簇索引“PK_dbo.AppUsers”,然后再创建另一个。

我正在开发Azure移动服务。

我有三种数据模型:

public class AppUser : EntityData { public string Username { get; set; } public virtual ICollection 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出现在迁移文件夹中,但不在表中。

在上下文文件中:

 public class ICbackendContext : DbContext { public DbSet AppUsers { get; set; } public DbSet PlaceItems { get; set; } public DbSet RatingItems { get; set; } 

}

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

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

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

有关详细信息,请参阅如何对.NET后端移动服务进行数据模型更改 。 该主题适用于移动服务和移动应用程序,但移动应用程序中的某些名称空间不同。

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

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

你的内心

 protected override void OnModelCreating(DbModelBuilder modelBuilder) 

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