In Entity Framework 6, under the DbContext class you can get to current transaction by context.Database.CurrentTransaction. However this API doesn't seem like available in EF.Core. How can I retrieve current transaction object from a dbcontext in EF.Core?
-
1I can't check the older versions, but in EF Core v1.1.0 you can use the same code as in EF6. – Ivan Stoev Dec 30 '16 at 19:04
-
Can you point me to .Core documentation where DbContext has these properties available? I don't see these. – BKS Dec 30 '16 at 19:15
-
1Sure, here you go docs.microsoft.com/en-us/ef/core/api/… – Ivan Stoev Dec 30 '16 at 19:22
-
That's beautiful. I am using EntityFramworkCore.Tools version 1.0.0preview2-final. Guess it was not in there. How can I know which version of documentation is it? – BKS Dec 30 '16 at 19:27
-
1Documentation is a tricky topic these days - it either lacks, or isn't up-to-date, not to say what is available since what version :( – Ivan Stoev Dec 30 '16 at 19:40
EF Core still lacks many EF6 features, but (slowly) is catching up.
In that regard, I can't say for earlier versions, but in the latest (so far ) v1.1.0 the DbContext.Database
CurrentTransaction
property is there, so you can use the same code as in EF6 to access it.
There isn't a method for retrieve the current transaction in Database property of DbContext, anbyway to start a transaction with EF Core, you can add this code:
using (var transaction = dbContext.Database.BeginTransaction())
{
try
{
// add code for ef core
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}
Let me know if this is useful.
-
My question is not about how to Begin, Commit or Rollback a transaction. So are you saying that its a missing functionality that was available in EF6 but not in EF.Core? – BKS Dec 30 '16 at 19:18
-
The link provided by @Ivan above clearly indicates that this is supported in latest version of EF Core. No offense but that's why I am voting negative to your answer. – BKS Dec 30 '16 at 19:45
-
1Don't worry about it, nice to know the issue it's solved in the last version of EF Core – H. Herzl Dec 30 '16 at 23:34