I'm trying to regroup a select followed by an update in an EFCore transaction. The entity I want to update have an id (For relation between entities) and a guid (To hide my predictable Ids). I don't want to use Guid as primary key.
When a user want to update the entity, I only got the guid from my DTO, then I must retrieve the Id from database to update my entity. At the moment, it works by doing a select and an update just after, but it's not optimized because it's doing two round trips to the database. How do you manage this kind of case without having to do two round trips ?
Here is my code atm :
public void Update(EmailTemplate emailTemplate, string username)
{
// Beginning of transaction
using (var transaction = _uow.BeginTransaction())
{
try
{
// Retrieving id by guid
emailTemplate.Id = _emailTemplateRepository.GetIdByGuid(emailTemplate.Guid);
emailTemplate.ModifiedBy = username;
emailTemplate.ModifiedDate = DateTime.Now;
foreach (EmailTemplateTranslation ett in emailTemplate.EmailTemplateTranslations)
{
// Updating childs
_emailTemplateTranslationRepository.Update(ett);
}
// Updating entity
_emailTemplateRepository.Update(emailTemplate);
// SaveChanges from UnitOfWork
_uow.Commit();
// Transaction commit
transaction.Commit();
}
catch(Exception ex)
{
transaction.Rollback();
}
}
}
This code generate two round trips even if the select is included in the transaction.