Currently, there is an option to save objects to the CosmosDB in transaction using Transactional Batch
string partitionKey = "The Family";
ParentClass parent = new ParentClass(){ Id = "The Parent", PartitionKey = partitionKey, Name = "John", Age = 30 };
ChildClass child = new ChildClass(){ Id = "The Child", ParentId = parent.Id, PartitionKey = partitionKey };
TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(parent.PartitionKey))
.CreateItem<ParentClass>(parent)
.CreateItem<ChildClass>(child);
TransactionalBatchResponse batchResponse = await batch.ExecuteAsync();
Unfortunately, when I tried to switch to EF Core I realized that context's SaveChangesAsync doesn't use TransactionalBatch and therefore it saves entities separately.
It is a problem in a situation when we change one entity and add another one:
- Book entity is blocked
- Reservation entity is created
When updating the Book entity fails because of ETag concurrency, creating the Reservation entity succeeds and we end up with an invalid state.
Would it be possible to introduce TransactionalBatch functionality to EF Core?
Currently, there is an option to save objects to the CosmosDB in transaction using Transactional Batch
Unfortunately, when I tried to switch to EF Core I realized that context's
SaveChangesAsyncdoesn't useTransactionalBatchand therefore it saves entities separately.It is a problem in a situation when we change one entity and add another one:
When updating the Book entity fails because of ETag concurrency, creating the Reservation entity succeeds and we end up with an invalid state.
Would it be possible to introduce
TransactionalBatchfunctionality to EF Core?