linq2db / linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
MIT License
462 stars 38 forks source link

how to use transactions? #308

Closed aloksharma1 closed 2 weeks ago

aloksharma1 commented 1 year ago

Hello sorry for noob question, but i am consufed whether i should use linq2db transactions or ef core transaction when using in scenarios updateasync mergeasync and in some scenarios i am also doing changes using ef core savechangesasync

for e.g.

//add something using ef core
var AddSomething = await _repository.AddAsync(new MyModel
            {
                columns=values...,
            });
await _repository.SaveChangesAsync();

//then update using ef or merge using linqtodb based on findings
var UpdateSomething = await _otherrepository.NoTrackingQuery().Where(x => x.Id == request.Data && x.IsActive).UpdateAsync(x => new MyOtherModel
            {
                 columns=values...,                 
                DateModified = DateTime.UtcNow,
                IsModified = true
            });

how to wrap this mixed scenario in transactions?

sdanyliv commented 1 year ago

Have you tried to use EF Core transactions? Extension is trying to reuse connection from EF Core's DbContext.

aloksharma1 commented 1 year ago

so just using ef core transaction is enough, i dont need to add linq2db ones? i did put ef core transaction in my code, just not sure how or where to put linq2db transaction.

MaceWindu commented 2 weeks ago

Transactions managed by server/connection and linq2db doesn't have any special transaction handling code. It is completely fine to use ef transaction - linq2db inherits connection from EF context with it's transaction.