MySql.EntityFrameworkCore No DbUpdateConcurrencyException thrown when concurrency conflictis expected
Posted by:
zhenhua li
Date: April 10, 2024 08:03PM
当使用 MySql.EntityFrameworkCore 连接mysql 时候,更新数据出现并发的时候 也没有 抛出 DbUpdateConcurrencyException 异常信息
,代码如下
services.AddDbContext<testContext>(
(sp,optionsBuilder) =>
{
optionsBuilder.UseMySQL(connectionString)
.EnableDetailedErrors()
.EnableSensitiveDataLogging();
});
[Table("user_account2")]
public class UserAccount
{
[Column("id")]
public int Id { get; set; }
[Column("user_id")]
public int? UserId { get; set; }
[StringLength(11)]
//[Comment("账号名称")]
[Column("account_name")]
public string AccountName { get; set; }
//[Comment("创建时间")]
[Column("create_time")]
public DateTime? CreateTime { get; set; }
[Column("is_delete")]
public int? IsDelete { get; set; }
[Column("delete_time")]
public DateTime? DeleteTime { get; set; }
[Column("version", TypeName = "varchar")]
//[Timestamp]
[ConcurrencyCheck]
//[Timestamp]
public Guid Version { get; set; }
[Column(name: "create_at", TypeName = "timestamp")]
public DateTime? CreateAt { get; set; }
}
UserAccount? userAccount = testContext.UserAccounts.Find(100003);
//Console.WriteLine(userAccount.CreateAt?.ToString("yyyy-MM-dd HH:mm:ss.fff:ffffff"));
try
{
userAccount.AccountName = "atest";
userAccount.Version = Guid.NewGuid();
userAccount.CreateAt= DateTime.UtcNow;
testContext.UserAccounts.Update(userAccount);
int rows= testContext.SaveChanges();
Console.WriteLine(rows);
}
catch (DbUpdateConcurrencyException ex)
{
throw ex;
}
执行结果永远都还返回是1,没有抛出 DbUpdateConcurrencyException 异常
Subject
Written By
Posted
MySql.EntityFrameworkCore No DbUpdateConcurrencyException thrown when concurrency conflictis expected
April 10, 2024 08:03PM
Sorry, only registered users may post in this forum.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.