1. EF Connection String

2. this is my connection string using ADO.NET

how to re use connection string in EF using ADO.NET
1. EF Connection String

2. this is my connection string using ADO.NET

how to re use connection string in EF using ADO.NET
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted Jul 21, 2023, 8:08 PM EDT
In ADO.NET, Entity Framework (EF) connection strings can be reused by extracting the connection string from the EF DbContext and then using it to create a new ADO.NET SqlConnection object. Here's how you can do it:
First, you need to obtain the connection string from the EF DbContext. If you have already configured your EF DbContext and are using the DbContext class, you can access the connection string through its Database property.
using System.Data.Entity; // Assuming you have an existing DbContext instance called "dbContext" string efConnectionString = dbContext.Database.Connection.ConnectionString;If you are using EF Core (Entity Framework Core), the process is slightly different:
using Microsoft.EntityFrameworkCore; // Assuming you have an existing DbContext instance called "dbContext" string efConnectionString = dbContext.Database.GetDbConnection().ConnectionString;Create ADO.NET SqlConnection:
Now that you have the EF connection string, you can use it to create a new ADO.NET SqlConnection object and use it in your ADO.NET code.
using System.Data.SqlClient; string efConnectionString = "Your EF Connection String"; // Create a new SqlConnection using the EF connection string using (SqlConnection sqlConnection = new SqlConnection(efConnectionString)) { try { // Open the connection sqlConnection.Open(); // Perform ADO.NET operations using this SqlConnection // For example: using (SqlCommand sqlCommand = new SqlCommand("SELECT * FROM YourTable", sqlConnection)) { using (SqlDataReader reader = sqlCommand.ExecuteReader()) { while (reader.Read()) { // Process data here } } } // Remember to close the connection when done sqlConnection.Close(); } catch (Exception ex) { // Handle exceptions here } }By reusing the EF connection string, you can leverage the same database connection for both Entity Framework and ADO.NET operations within the same application, which can be useful when you need to perform low-level data access or optimize specific queries while still benefiting from the high-level abstraction that EF provides. However, keep in mind that mixing different data access approaches should be done with caution to avoid potential issues.
Rolando Puno137 reputationPosted Jul 22, 2023, 9:21 PM EDT
Thank you so much