0

当我在Azure .Net后端制作迁移文件时,它会自动生成五个必填字段(Id,CreatedAt,UpdatedAt,Version,Deleted)和我的客户字段。它将Id定义为主键。更改EF6中的主键名称

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作为主键。

祝你好运。