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?
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.
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.
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!!
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With