Duplicating record in Insert (Add or AddAsync)
See original GitHub issueThe System runs in Private Cloud and in the test environment I have no problem, but in the production environment at times the Insert duplicates the information generating the same record but as subsequent keys.
The code demonstrates what I basically do using the layer with Entity Framework. I’ve done several tests and I can’t understand why this duplication sometimes in the registrations, I have a network with more than 20 people working on this Web system
Example
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(AccountSchedule model)
{
if (ModelState.IsValid)
{
using IDbContextTransaction transaction = UnitOfWork.BeginTransaction();
try
{
UnitOfWork.AccountSchedule.Add(model);
UnitOfWork.Commit();
transaction.Commit();
TempData.AddSuccessCreate();
return RedirectToAction(nameof(Edit), new { model.Id });
}
catch (Exception)
{
transaction?.Rollback();
}
}
ViewData.AddError();
await LoadSelectAsync(model);
return View("CreateOrUpdate");
}
Code
var options = new DbContextOptionsBuilder<DataAccess>();
options.UseMySQL("Server=localhost;Database=dB;Uid=test;Pwd=232367");
options.LogTo(Console.WriteLine);
using DataAccess dataAccess = new DataAccess(options.Options);
using var transaction = dataAccess.Database.BeginTransaction();
AccountSchedule accountSchedule = new()
{
AccountTypeId = 1,
Description = "Example Issue",
Value = 10.9M,
ExpirationDay = 10,
Active = true
};
dataAccess.AccountSchedule.Add(accountSchedule);
dataAccess.SaveChanges();
transaction.Commit();
Ouput SQL
Executing DbCommand [Parameters=[
@p0='?' (DbType = Int32),
@p1='?' (DbType = SByte),
@p2='?' (DbType = DateTime),
@p3='?' (Size = 300),
@p4='?' (DbType = Int32),
@p5='?' (DbType = Decimal)], CommandType='Text', CommandTimeout='30']
INSERT INTO `account_schedule` (`account_type_id`, `active`, `deleted_at`, `description`, `expiration_day`, `value`)
VALUES (@p0, @p1, @p2, @p3, @p4, @p5);
SELECT `id`
FROM `account_schedule`
WHERE ROW_COUNT() = 1
AND `id`=LAST_INSERT_ID();
Further technical details
MySQL version: 8.0.28 Operating system: Windows Server 2019 Standard Edition Pomelo.EntityFrameworkCore.MySql version: 5.0.4 Microsoft.AspNetCore.App version: net5.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:16
Top Results From Across the Web
Entity Framework Core is trying to insert duplicated records ...
When I call SaveChanges() the EF is trying to insert two identical records for ShoppingCartItem . The ShoppingCartItem is created and added to ......
Read more >Why is EF core 5.0 inserting duplicate records
Why is Entity Framework 5.0 inserting duplicate records in the Database in uncertain behavior when using AddAsync() method for adding data.
Read more >Why is EF core 5.0 inserting duplicate records?
Why is Entity Framework 5.0 inserting duplicate records in the Database in uncertain behavior when used .AddAsync() method for adding data.
Read more >Duplicating Related Entities using Entity Framework Core
Using this idea, we can copy data by fetching an entity, detaching the entity, and then adding it back to the database (and...
Read more >I need an approach to the problem of preventing inserting ...
In visual studio, I updated my eddmx (Entity framework file), but when I tried to insert duplicate records, all I got was another...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Any soft delete functionality should be implemented in EF Core not at the provider level, i.e. this is universal, not provider specific.
Any soft delete functionality should be addressed upstream, it’s under consideration in https://github.com/dotnet/efcore/issues/22959 so please upvote.