MySql.EntityFrameworkCore No DbUpdateConcurrencyException thrown when concurrency conflictis expected
Posted by:
zhenhua li
Date: April 10, 2024 08:05PM
When using MySql When the EntityFrameworkCore connects to MySQL, it does not throw the DbUpdateConcurrencyException exception when there is concurrency in updating data
The code is as follows
Services AddDbContext<testContext>(
(sp, optionsBuilder)=>
{
OptionsBuilder UseMySQL (connectionString)
. Enabling DetailedErrors ()
. enableSensitiveDataLogging();
});
[Table ("user_count2")]
Public class UserAccount
{
[Column ("id")]
Public int Id {get; set;}
[Column ("user_id")]
Public int? UserId {get; set;}
[StringLength (11)]
//[Comment ("Account Name")]
[Column ("account name")]
Public string AccountName {get; set;}
//[Comment ("Creation Time")]
[Column ("create-time")]
Public DateTime? Create Time {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: "createat", TypeName="timestamp")]
Public DateTime? Create At {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 Create At=DateTime UtcNow;
TestContext UserAccounts Update (userAccount);
Int rows=testContext SaveChanges ();
Console WriteLine (rows);
}
Catch (DbUpdateConcurrencyException ex)
{
Throw ex;
}
The execution result always returns 1, and no DbUpdateConcurrencyException exception was thrown
Subject
Written By
Posted
MySql.EntityFrameworkCore No DbUpdateConcurrencyException thrown when concurrency conflictis expected
April 10, 2024 08:05PM
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.