Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass IDbConnection to EF context?

Entity Framework cotext DbContext has constructor which takes DbConnection. Why not IDbConnection?

Is it normally to pass DbConnection to EF context instead of using default constructor (which reads connection string from App.config?)

like image 804
deeptowncitizen Avatar asked Apr 12 '15 16:04

deeptowncitizen


People also ask

Can we use Dapper with Entity Framework?

To demonstrate the usage of Dapper, Entity Framework Core, and both combined, we will implement them each in the 3 Endpoints. For the GetAll Endpoints, we will use Dapper. The GetById Endpoint would use Entity Framework Core with Eager Loading to display the Department Details as well.

Is EF core faster than EF6?

EF Core 6.0 itself is 31% faster executing queries. Heap allocations have been reduced by 43%.

How do I refresh EF context?

The best way to refresh entities in your context is to dispose your context and create a new one.

How do I change the connection string in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

How to use dbcontext in EF?

The EF uses this section to locate the database to use for the context. You can use this section change any provider or you can use customer providers. The DbContext also takes the string constructor. You can either pass the name of the database or the connection string to the constructor

Is it possible to pass connections to Entity Framework context?

Privacy policy. Thank you. This page describes the behavior of Entity Framework with regard to passing connections to the context and the functionality of the Database.Connection.Open () API. There are two constructors which accept connections: It is possible to use these but you have to work around a couple of limitations:

Can EF Core use the same connection for connected and disconnected transactions?

ef core can use same connection or different or based on connection pooling. Ef core has connected and disconnected mode of transaction. I think this can suit you. aving data in the disconnected scenario is a little bit different than in the connected scenario.

What is the purpose of the connection section in EF?

The section is under the section and is required and is used if you do not specify the connection string. The EF uses this section to locate the database to use for the context. You can use this section change any provider or you can use customer providers.


1 Answers

Entity Framework cotext DbContext has constructor which takes DbConnection. Why not IDbConnection?

As i'm not part of the EF team, i can't say anything on their behalf. It seems to me like DbConnection, as an abstract base already has some of the implementations made for you, so you any overriden class gets things "for free".

I've found a decent answer here:

One of the main benefits has to do with versioning. As Microsoft adds new functionality to the providers they need to expose this functionality to the developer.

Using the old interface only approach, they would have to change the interface ( but they wouldn't because interfaces are immutable ) and all class implementing the interface in the framework. Any custom classes written by Developers implementing the interface would also need to be modified to implement any changes to the interface. If they keep adding new features ( which they will ), this becomes a vicious cycle of needing to change interfaces or implementing new ones to offer new functionality.


Is it normally to pass DbConnection to EF context instead of using default constructor?

The docs are pretty clear on why you'd use this overload:

Constructs a new context instance using the existing connection to connect to a database. The connection will not be disposed when the context is disposed if contextOwnsConnection is false.

If you already have an open context and would like to pass that around, you can do so. "Is it normal" will vary for each use case. I myself haven't used this overload at all.

like image 132
Yuval Itzchakov Avatar answered Oct 22 '22 14:10

Yuval Itzchakov