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核心的限制还是我试图做到这一点不正确?