0

Azure .Netバックエンドでマイグレーションファイルを作成すると、顧客フィールドに5つの必須フィールド(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); 

    } 

私は「PERSONID」にこのプライマリキー名「ID」を変更したいので、私は

public class Person : EntityData 
{ 
    public string PersonId; 
    .... 
} 

以下のように主キープロパティを追加します。しかし、それは動作しませんでした。 「Id」は「PersonId」に置き換えられず、マイグレーションファイルの顧客フィールドとして追加されました。そこで、[Key]属性を追加しましたが、エラーが発生し、以下のメッセージが表示されます。

"複合プライマリキーの順序を指定するには、ColumnAttributeまたはHasKeyメソッドを使用します。

私は主キーの名前をどのように変更できますか?

+0

名前を変更した列に対しては、手動で移行コードを追加する必要があります。 –

答えて

1

HasKeyメソッドを使用して、プロパティをプライマリキーに設定できます。例えば

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

それは主キーとしてPERSONIDを設定します。

Good Luck。