开发者

Accessing Connection property using DbContext

I am upgrading my practice Entity Framework code to v4.1. In old version, I had my context class deriving from ObjectContext but with new release of EF 4.1, they have provided a good DbContext API.

I am basically trying to convert the code so that it works from Database First approach to Code First approach. Playing around with EF 4.1

In old code, I had something like

context.Connection.BeginTransaction(isolationLevel);

where the context Type was deriving from Obje开发者_C百科ctContext.

In v4.1 I haven't got access to Connection property from context. How can I do that?


It is in the DbContext and it should be public.

dbContext.Database.Connection.ConnectionString

also:

dbContext.Database.Connection.BeginTransaction(isolationLevel)


In the brave new .Net Core world, you can use:

using Microsoft.EntityFrameworkCore;

context.Database.GetDbConnection();


The new DBContext has

context.Database.Connection.BeginTransaction() 

method with some overloads. Do a Goto Definition in Visual Studio to see the methods.


You can use something like

((IObjectContextAdapter)context).ObjectContext.Connection.BeginTransaction(isolationLevel);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜