Sil*_*lez 8 postgresql entity-framework rowversion optimistic-concurrency
我在 PostgreSQL 中使用 Entity Framework 6。我有一个实体,我想在其中防止并发问题,按照本文档,我添加了一个带有 [Timestamp] 属性的 RowVersion 属性,但是在保存对实体的更改后,列 RowVersion 值在数据库中保持不变。
[Timestamp]
public byte[] RowVersion { get; set; }
Run Code Online (Sandbox Code Playgroud)
我是不是遗漏了什么,还是有其他方法可以在 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; }
Run Code Online (Sandbox Code Playgroud)
不得不为迁移中不添加的列添加 [NotMapped] 属性,在数据库更新后对其进行评论。
只是 EF Core 的更新答案,以防其他人在这里徘徊。
该Npgsql的框架已经使用隐藏的系统栏的内置支持这一XMIN的OP是他作为一个实体使用NotMapped属性。
如此处所述,您可以通过 Fluent APIUseXminAsConcurrencyToken在实体的方法中调用该方法,将 xmin 列设置为 EF 中的并发令牌OnModelCreating(据我所知,此时数据注释不可用)。
对于任何已经使用 Fluent API 配置的人来说,就像这样简单:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.ToTable("Awesomeness");
builder.HasKey(pk => pk.Id);
// ADD THIS LINE
builder.UseXminAsConcurrencyToken();
builder.Property(p => p.IsAwesome)
.IsRequired();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4256 次 |
| 最近记录: |