2017-10-18 7 views
1

まず、Entity Frameworkコードを使用して、SQL ServerのデータテーブルにTimestamp/rowversion列を追加する方法を理解しようとしています。EFコアを使用したTimeStamp/Rowversion列

これはEF Core V2.0を使用しています。

私はこれを達成するために2つのアプローチを試みました。

public class Contact 
{ 

    public int ContactId { get; set; } 

    public string ContactName { get; set; } 

    public string CompanyName { get; set; } 

    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    [Timestamp] 
    public byte[] RowVersion { get; set; } 
} 

Iアドインの移行私は次の取得を実行した後、両方のケースでは流暢なAPI

protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     foreach (var entityType in modelBuilder.Model.GetEntityTypes()) 
     { 
      modelBuilder.Entity(entityType.Name).Property<DateTime>("Inserted"); 
      modelBuilder.Entity(entityType.Name).Property<DateTime>("LastModified"); 
      modelBuilder.Entity(entityType.Name).Property<byte[]>("RowVersion"); 

     } 
    } 

を介して第2、

 protected override void Up(MigrationBuilder migrationBuilder) 
    { 
     migrationBuilder.CreateTable(
      name: "Contacts", 
      columns: table => new 
      { 
       ContactId = table.Column<int>(type: "int", nullable: false) 
        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), 
       CompanyName = table.Column<string>(type: "nvarchar(max)", nullable: true), 
       ContactName = table.Column<string>(type: "nvarchar(max)", nullable: true), 
       FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true), 
       Inserted = table.Column<DateTime>(type: "datetime2", nullable: false), 
       LastModified = table.Column<DateTime>(type: "datetime2", nullable: false), 
       LastName = table.Column<string>(type: "nvarchar(max)", nullable: true), 
       RowVersion = table.Column<byte[]>(type: "varbinary(max)", nullable: true) 
      }, 
      constraints: table => 
      { 
       table.PrimaryKey("PK_Contacts", x => x.ContactId); 
      }); 
    } 

RowVersion列の型がタイムスタンプであり、nullableプロパティがfalseであることが予想されます。これはEF Coreの制限ですか、これを間違って実行しようとしていますか?

答えて

0
protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    base.OnModelCreating(modelBuilder); 

    modelBuilder.Entity<MyEntity>() 
     .Property(b => b.MyVersionProperty) 
     .IsRowVersion(); 
} 
関連する問題