Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 451 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing Connection property using DbContext

#1
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 ObjectContext.

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

#2
You can use something like

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


Reply

#3
The new DBContext has

context.Database.Connection.BeginTransaction()

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

#4
It is in the DbContext and it should be public.

dbContext.Database.Connection.ConnectionString

also:

dbContext.Database.Connection.BeginTransaction(isolationLevel)
Reply

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

```
using Microsoft.EntityFrameworkCore;

context.Database.GetDbConnection();
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through