当我在Azure .Net Backend中创建迁移文件时,它会自动生成包含我的客户字段的五个必填字段(Id,CreatedAt,UpdatedAt,Version,Deleted)。它将Id定义为主键。
public override void Up()
{
CreateTable(
"dbo.People",
c => new
{
Id = c.String(nullable: false, maxLength: 128,
annotations: new Dictionary<string, AnnotationValues>
{
{
"ServiceTableColumn",
new AnnotationValues(oldValue: null, newValue: "Id")
},
}),
PersonType = c.String(nullable: false, maxLength: 2),
FirstName = c.String(nullable: false, maxLength: 50),
MiddleName = c.String(maxLength: 50),
LastName = c.String(nullable: false, maxLength: 50),
Gender = c.String(nullable: false, maxLength: 1),
BirthDate = c.DateTime(nullable: false, storeType: "date"),
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);
}
我想将此主键名称“Id”更改为“PersonId”,因此我添加了一个主键属性,如下所示
public class Person : EntityData
{
public string PersonId;
....
}
但它没有用。 “Id”未被“PersonId”取代,只是作为客户字段添加到迁移文件中。所以,添加了[Key]属性,但是它出错了,我收到了以下消息。
“无法确定类型的复合主键排序。使用ColumnAttribute或HasKey方法指定复合主键的顺序。”
如何更改主键名称?
答案 0 :(得分:1)
您可以使用HasKey方法将属性设置为主键。
例如,
modelBuilder.Entity<Person>().HasKey(t => t.PersonId);
它将PersonId设置为主键。
祝你好运。