Skip to content

Transactional Batch support for CosmosDB in EF Core #23364

@rmaziarka

Description

@rmaziarka

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?

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions