Home > database >  Can we use Entity Framework and ADO.NET together in the same project?
Can we use Entity Framework and ADO.NET together in the same project?

Time:01-21

ASP.NET Core .NET 6

  • I would like to use Identity (using the default EF configuration) for my web app project
  • At the same time, I also want to use ADO.NET or Dapper quite extensively for database queries
  • Both EF and ADO.NET will use the same connection string

My questions:

  • Will there be any problems such as clash in connections?

I tried ADO.NET in ASP.NET Core 6 and it works nicely, and used it to execute stored procedures.

CodePudding user response:

It will work fine. An EF DbContext will open and close connections as needed. Dapper/ADO.NET can open and close connections using the same connection string, and they will all share the same connection pool.

They can even share the same connection. Either pass the DbConnection into the DbContext constructor, or let the DbContext manage the connection, and call

var con = this.Database.GetDbConnection();
if (con.State != System.Data.ConnectionState.Open)
    con.Open();

To get the DbConnection and ensure that it stays open for the lifetime of the DbContext.

  • Related