aimprogman
aimprogman

Reputation: 294

Entity Framework Core pessimistic locking row table Mysql

How can I use sql query in EF Core as for example this._context.Person.FirstOrDefault(a => a.id==1).ForUpdate()

SELECT * FROM person WHERE id = 1 FOR UPDATE;

in EF core I find Implementing optimistic concurrency with EF Core( [ConcurrencyCheck], with Fluent Api ".IsConcurrencyToken();"), but it not solved my problem

Upvotes: 5

Views: 1535

Answers (2)

aimprogman
aimprogman

Reputation: 294

To resolve my question i use this aproach:

this._context.Person.FromSql($"SELECT * FROM person WHERE id= { personId } FOR UPDATE").FirstOrDefaultAsync();

In Github has already been opened issue https://github.com/aspnet/EntityFrameworkCore/issues/6717

When this issue will resolved(closed), then we can do as I want

Upvotes: 0

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30565

you can use TransactionScope under System.Transactions

using (var scope = new TransactionScope())
{
  var person = this._context.Person.FirstOrDefault(a => a.id==1);
  person.Col1 = "John Doe";
  this._context.Person.Update(person);

  this._context.SaveChanges();
  scope.Complete();
}

Upvotes: 1

Related Questions