1
我想弄清楚我應該如何通過實體框架代碼首先將時間戳/ rowversion列添加到sql服務器中的數據表中。帶EF核心的TimeStamp/Rowversion列
這是使用EF Core V2.0。
我試過兩種方法來實現這個,第一種;
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; }
}
通過流暢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");
}
}
第二個在這兩種情況後,我運行Add-遷移,我得到以下;
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列是類型時間戳,並且其可爲空屬性爲false。這是EF核心的限制還是我試圖做到這一點不正確?