Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Entity Framework Connection String

We use Entity Framework 5, but have a requirement to ALSO use a normal database connection from the application for some custom SQL we need to perform.

So, I am creating a DatabaseAccess class which handles this connection. Is there a way that I can populate the connection string, by checking the Entity Framework connection string?

So:

SqlConnection cnn; connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" 

Can I build that from checking Entity Framework?

like image 279
Craig Avatar asked Oct 22 '12 01:10

Craig


People also ask

What is connection string in Entity Framework?

A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.

How do I get connection string?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.

How do I test my EDMX connection string?

Open the edmx (go to properties, the connection string should be blank), close the edmx file again. Open the app. config and uncomment the connection string (save file) Open the edmx, go to properties, you should see the connection string uptated!!

What is a connection string in Entity Framework?

Connection Strings in the ADO.NET Entity Framework. A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection.

How does dbcontext connect to the database in Entity Framework?

The DBContext connects to the database using the Database Providers. These Providers requires a connection string to connect to the database. The way the connection string is specified has changed from the previous version of the entity framework. There are several ways by which you can provide the connection string to EF Core application.

How do I access entityconnection in ObjectContext?

The EntityConnection used by an ObjectContext instance can be accessed from the Connection property. For more information, see Managing Connections and Transactions. To learn about the general syntax for connection strings, see Connection string syntax | Connection Strings in ADO.NET.

Is the providername setting required on EF Core connection strings?

The providerName setting is not required on EF Core connection strings stored in App.config because the database provider is configured via code. You can then read the connection string using the ConfigurationManager API in your context's OnConfiguring method.


1 Answers

You can get the connectionstring used by EF by using the following:

MyDbContext.Database.Connection.ConnectionString 

Or as mark says you can initalise the context with a sqlconnection

like image 141
Not loved Avatar answered Nov 07 '22 16:11

Not loved