我将 Entity Framework 6 与 PostgreSQL 一起使用。我有一个实体,我想防止并发问题,在此文档之后,我添加了一个带有 [Timestamp] 属性的 RowVersion 属性,但是在保存对实体的更改后,列 RowVersion 值在数据库中保持不变。
[Timestamp]
public byte[] RowVersion { get; set; }
我错过了什么还是在 PostgreSQL 中有另一种方法来处理它?
我将 Entity Framework 6 与 PostgreSQL 一起使用。我有一个实体,我想防止并发问题,在此文档之后,我添加了一个带有 [Timestamp] 属性的 RowVersion 属性,但是在保存对实体的更改后,列 RowVersion 值在数据库中保持不变。
[Timestamp]
public byte[] RowVersion { get; set; }
我错过了什么还是在 PostgreSQL 中有另一种方法来处理它?
只是 EF Core 的更新答案,以防其他人在这里徘徊。
Npgsql框架使用隐藏的系统列xmin对此提供内置支持,OP 在其实体中将其用作NotMapped
属性。
如此处所引用,UseXminAsConcurrencyToken
您可以通过 Fluent API在其方法中调用实体上的方法,将 xmin 列设置为 EF 中的并发标记OnModelCreating
(据我所知,此时数据注释不可用)。
对于任何已经使用 Fluent API 配置的人来说,它就像这样简单:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.UseXminAsConcurrencyToken();
}
}
/// <summary>
/// Meant to validate concurrency en database update
/// This column is updates itself in database and only works in postgresql
/// </summary>
[ConcurrencyCheck]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
//[NotMapped]
public string xmin { get; set; }
必须为迁移中不添加的列添加 [NotMapped] 属性,在数据库更新后对其进行评论。