admin管理员组文章数量:1410717
Problem: when saving a simple entity to the SQL database, it is occasionally not persisted.
I'm using a .NET application using
Microsoft.EntityFrameworkCore.SqlServer
v 9.0.2.
The logic is that I check if a record exists in the database table, and if not, then adds it.
My code that adds the record is:
MyEntity e = new()
{
abc = "abc",
etc = etc,
};
dbContext.MyEntities.Add(e);
try
{
await dbContext.SaveChangesAsync(cancellationToken);
}
catch (DbUpdateException e)
{
if (e.InnerException?.Message.Contains("Violation of Primary Key", StringComparison.OrdinalIgnoreCase) ?? false)
{
// Subject of a race-condition, another thread got here first.
}
else
{
// Try again
try
{
await dbContext.SaveChangesAsync(cancellationToken);
}
catch
{
logger.LogError(e, "Failed to save changes.");
throw;
}
}
}
catch (Exception e)
{
// Try again
try
{
await dbContext.SaveChangesAsync(cancellationToken);
}
catch
{
logger.LogError(e, "Failed to save changes.");
throw;
}
}
No exception is thrown.
The vast majority of times this works perfectly but under times (possibly of heavy load?) this is running without throwing an exception, but the entity is not added to the table.
Admittedly, the code is not looking at the return value (which should be 1).
Before making any changes to the code, just wondered if anyone could see any reasons why this wouldn't execute as expected? My expectation is that either that the item was already there, it is added to the database, or it errors.
The DbContext
had a lifetime of Scoped
, but I've changed that to Transient
(zero effect).
My next approach is to look at the INT returned from
await dbContext.SaveChangesAsync(cancellationToken)
and if it's not 1 then throw an exception (unless it was already there).
本文标签: Issue with saving an entity using Entity Framework CoreStack Overflow
版权声明:本文标题:Issue with saving an entity using Entity Framework Core - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745023947a2638294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论