Hello Community,
I have a question if its possible to have database transaction scope using ESQ instead of direct SQL. From the documentation it is suggested that we as Devs should use ESQ most of the times because of validation etc.
I have a case where I have to modify two entities and for that I need two separate ESQ (maybe there is a better way). My goal is to wrap that in a single transaction so that all changes are made or none (to avoid inconsistency).
Is this code below valid for this case? Is going to work?
```
using System;
using Terrasoft.Core.DB;
using Terrasoft.Core.Entities;
using (DBExecutor dbExecutor = UserConnection.EnsureDBConnection())
{
dbExecutor.StartTransaction();
try
{
var schemaManager = UserConnection.EntitySchemaManager;
// Contact
Entity contact = schemaManager.GetInstanceByName("Contact").CreateEntity(UserConnection);
contact.SetDefColumnValues();
contact.SetColumnValue("Name", "Jane Doe");
contact.Save();
// Case, linked to the new contact
Entity supportCase = schemaManager.GetInstanceByName("Case").CreateEntity(UserConnection);
supportCase.SetDefColumnValues();
supportCase.SetColumnValue("Subject", "Test case");
supportCase.SetColumnValue("ContactId", contact.PrimaryColumnValue);
supportCase.Save();
// ...more entities as needed
dbExecutor.CommitTransaction();
}
catch
{
dbExecutor.RollbackTransaction();
throw;
}
}
```
Thanks for all answers.