question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicating record in Insert (Add or AddAsync)

See original GitHub issue

The 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:open
  • Created 2 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
mguinnesscommented, Feb 25, 2022

Any soft delete functionality should be implemented in EF Core not at the provider level, i.e. this is universal, not provider specific.

1reaction
mguinnesscommented, Feb 22, 2022

I even have a package for EF that does a non-physical deletion of the record, would that be too difficult to put in Pomelo? (https://www.nuget.org/packages/Canducci.SoftDelete/)

Any soft delete functionality should be addressed upstream, it’s under consideration in https://github.com/dotnet/efcore/issues/22959 so please upvote.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found