Ends in:
3 DAYS
22 HRS
10 MIN
58 SEC
Ends in:
3 D
22 H
10 M
58 S
Open Closed

UnitofWorkManager does not manage the entity that is outside its scope #4721


User avatar
0
ademaygun created
  • ABP Framework version: v5.3.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes / no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"
  • Create new solution (Abp Suite)
  • Create a new entity called Book
  • Add a new property called Name

when the await uow.CompleteAsync(); runs I observe that there is a change in the database

public virtual async Task UpdateNameAsync(Guid id)
{
	using var uow = unitOfWorkManager.Begin(requiresNew: true, isTransactional: true);
	var book = await _bookRepository.GetAsync(id);
	book.Name = DateTime.Now.ToString();
	await uow.SaveChangesAsync();
	await uow.CompleteAsync();
	throw new Exception("an occured exception")
}

but when the await uow.CompleteAsync(); runs in the code below, there is no change in the database. Surrounding unit of work manages book entity (instead of new uow)

public virtual async Task UpdateNameAsync(Guid id)
{
	var book = await _bookRepository.GetAsync(id);
	using var uow = unitOfWorkManager.Begin(requiresNew: true, isTransactional: true);
	book.Name = DateTime.Now.ToString();
	await uow.SaveChangesAsync();
	await uow.CompleteAsync();
	throw new Exception("an occured exception")
}

My original code similar like below. I want database changes to commit even though Exception is thrown.

public virtual async Task UpdateNameAsync(Guid id)
{
	var book = await _bookRepository.GetAsync(id);
	await UpdateNameFunctionAsync(book);
}

private async Task UpdateNameFunctionAsync(Book book)
{
	var uow = unitOfWorkManager.Begin(requiresNew: true, isTransactional: true);
	book.Name = DateTime.Now.ToString();
	await uow.SaveChangesAsync();
	await uow.CompleteAsync();
	throw new Exception("an occured exception");
}

Would you consider this a bug?


1 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Different units of work use different dbcontxt, so you have to put code in one unit of work.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 02, 2024, 12:35