不能在一个表上创建多个聚集索引 [英] Cannot create more than one clustered index on table

查看:867
本文介绍了不能在一个表上创建多个聚集索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

键入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆