更改 EF6 中的主键名称

Changing the primary key name in EF6

当我在 Azure .Net 后端创建迁移文件时,它会自动生成五个必填字段(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] 属性,但它出错了,我收到了以下消息。

"Unable to determine composite primary key ordering for type . Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys."

如何更改主键名称?

您可以使用 HasKey 方法将 属性 设置为主键。

例如,

modelBuilder.Entity<Person>().HasKey(t => t.PersonId);

它将PersonId设置为主键。

祝你好运。