我在PostgreSQL中使用实体框架6。我有一个实体希望防止并发问题,在这份文件之后,我添加了一个带有时间戳属性的RowVersion属性,但是在保存对实体的更改后,列RowVersion值将保持在数据库中相同的位置。
[Timestamp]
public byte[] RowVersion { get; set; }我是遗漏了什么,还是在PostgreSQL中有其他方法来处理它?
发布于 2017-09-08 13:46:27
/// <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属性,只是为了在迁移中不添加列,在数据库更新后对其进行注释。
发布于 2019-08-14 13:13:44
只是更新了EF核心的答案,以防其他人在这里游荡。
Npgsql框架内置了对此的支持,使用OP在其实体中使用的隐藏系统列xmin作为NotMapped属性。
作为引用的这里,您可以将xmin列设置为EF中的并发令牌,方法是通过Fluent API在实体的OnModelCreating方法中调用UseXminAsConcurrencyToken方法(据我所知,此时无法使用数据注释)。
对于任何已经使用Fluent API配置的人来说,它非常简单:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.UseXminAsConcurrencyToken();
}
}https://stackoverflow.com/questions/42651101
复制相似问题