我的后端是用 .NET 构建的,通过在解决方案中包含一个表,我收到以下错误:
Cannot create more than one clustered index on table 'dbo.Favorit'. Drop the existing clustered index 'PK_dbo.Favorit' before creating another.
此代码是在执行 Add-Migration CreateFavorit
和 update-database
命令后生成的:
namespace appService.Migrations
{
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.Migrations;
public partial class CreateFavorit : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Favorit",
c => new
{
Id = c.String(nullable: false, maxLength: 128,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Id")
},
}),
Nome = c.String(),
Lat_dest = c.Double(nullable: false),
Lon_dest = c.Double(nullable: false),
Id_usuario = c.String(),
Endereco = c.String(),
MeioTransporte = c.String(),
Id_usuario_2 = c.String(),
Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion",
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Version")
},
}),
CreatedAt = c.DateTimeOffset(nullable: false, precision: 7,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "CreatedAt")
},
}),
UpdatedAt = c.DateTimeOffset(precision: 7,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "UpdatedAt")
},
}),
Deleted = c.Boolean(nullable: false,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Deleted")
},
}),
})
.PrimaryKey(t => t.Id)
.Index(t => t.CreatedAt, clustered: true);
}
public override void Down()
{
DropIndex("dbo.Favorit", new[] { "CreatedAt" });
DropTable("dbo.Favorit",
removedColumnAnnotations: new Dictionary<string, IDictionary<string, object>>
{
{
"CreatedAt",
new Dictionary<string, object>
{
{ "ServiceTableColumn", "CreatedAt" },
}
},
{
"Deleted",
new Dictionary<string, object>
{
{ "ServiceTableColumn", "Deleted" },
}
},
{
"Id",
new Dictionary<string, object>
{
{ "ServiceTableColumn", "Id" },
}
},
{
"UpdatedAt",
new Dictionary<string, object>
{
{ "ServiceTableColumn", "UpdatedAt" },
}
},
{
"Version",
new Dictionary<string, object>
{
{ "ServiceTableColumn", "Version" },
}
},
});
}
}
}
服务器microsoft-azure,数据库SQLServer。 怎么解决这个问题呢?或者,这个错误是什么? 谢谢。
编辑:
模型类:
namespace appService.DataObjects
{
public class Favorit : EntityData
{
public string Nome { get; set; }
public double Lat_dest { get; set; }
public double Lon_dest { get; set; }
public string Id_usuario { get; set; }
public string Endereco { get; set; }
public string MeioTransporte { get; set; }
public string Id_usuario_2 { get; set; }
}
}
最佳答案
我们可以在Configuration.cs文件中包含以下代码来解决它。
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
}
该错误消息是由 Entity Framework 没有用于创建非主键聚集索引的注释引起的。移动 SDK 手动创建正确的 SQL 语句以将 CreateAt
设置为非主键聚集索引。更多详细信息请引用另一篇SO thread .
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.
我测试了一下,运行正常。以下是我的详细步骤:
1.从azure门户下载移动项目
2.在项目中添加新模型
3.在XXXContext.cs文件中添加属性
4.在Configuration.cs文件中添加SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator())
5.在程序包管理器控制台中运行enable-migrations -force
、add-migration tomtest-somechange
、update-database
。
6.检查表是否正确创建
关于c# - 执行迁移后出错(.Net 后端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962943/