Entity Framework 上 PostgreSQL 的 RowVersion 实现
RowVersion implementation on Entity Framework for PostgreSQL
我正在使用 Entity Framework 6 和 PostgreSQL。
我有一个实体,我想在其中防止并发问题,在 this documentation 之后我添加了一个带有 [Timestamp] 属性的 RowVersion 属性,但是在保存对实体的更改后,列 RowVersion 值在数据库。
[Timestamp]
public byte[] RowVersion { get; set; }
我是不是遗漏了什么或者在 PostgreSQL 中有其他方法可以处理它?
/// <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] 属性,在数据库更新后对其进行注释。
只是 EF Core 的更新答案,以防其他人在这里徘徊。
Npgsql 框架使用隐藏系统列 xmin 对此提供了内置支持,OP 在其实体中将其用作 NotMapped
属性.
如引用 here 所述,您可以通过 Fluent [=26] 在实体的 OnModelCreating
方法中调用实体的 UseXminAsConcurrencyToken
方法,将 xmin 列设置为 EF 中的并发标记=](据我所知,此时数据注释不可用)。
对于任何已经使用 Fluent API 配置的人来说,就这么简单:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.UseXminAsConcurrencyToken();
}
}
我正在使用 Entity Framework 6 和 PostgreSQL。 我有一个实体,我想在其中防止并发问题,在 this documentation 之后我添加了一个带有 [Timestamp] 属性的 RowVersion 属性,但是在保存对实体的更改后,列 RowVersion 值在数据库。
[Timestamp]
public byte[] RowVersion { get; set; }
我是不是遗漏了什么或者在 PostgreSQL 中有其他方法可以处理它?
/// <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] 属性,在数据库更新后对其进行注释。
只是 EF Core 的更新答案,以防其他人在这里徘徊。
Npgsql 框架使用隐藏系统列 xmin 对此提供了内置支持,OP 在其实体中将其用作 NotMapped
属性.
如引用 here 所述,您可以通过 Fluent [=26] 在实体的 OnModelCreating
方法中调用实体的 UseXminAsConcurrencyToken
方法,将 xmin 列设置为 EF 中的并发标记=](据我所知,此时数据注释不可用)。
对于任何已经使用 Fluent API 配置的人来说,就这么简单:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.UseXminAsConcurrencyToken();
}
}