问题
Given the following connection string:
<add name="PrimaryDBConnectionString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=10.1.1.101;Initial Catalog=primary;Persist Security Info=True;User ID=myuserid;Password=mypassword;MultipleActiveResultSets=True;Application Name=EntityFramework"" />
I attempt to open a connection in my DAL with the following:
using (PrimaryDBContext ctx = new PrimaryDBContext(ConfigurationManager.ConnectionStrings["PrimaryDBConnectionString"].ToString()))
{
try
{
ctx.Connection.Open();
var result = ctx.sq_newsfeed_GetProfileByID(profileID);
The error I get is:
The underlying provider failed on Open
I have messed around with the EF connection string and replaced all the provider prefix stuff with "metadata=res://*/;" but still no go.
Can anyone shed some light on this please?
Thanks.
-- Update --
Thank you for the response... I ended up just creating a new db connection from the UI and modifying the connection string to match my needs:
<add name="PrimaryEntities" connectionString="metadata=res://*/PrimaryModel1.csdl|res://*/PrimaryModel1.ssdl|res://*/PrimaryModel1.msl;provider=System.Data.SqlClient;provider connection string="data source=10.99.108.42;initial catalog=primary;user id=dbuserID;password=somepw;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I kept the metadata portion. The trick was to ensure that your .csdl, .ssdl and .msl file name prefix matches your db context.
回答1:
Are you using Code First/DbContext (your question is filed under entity-framework-4.1)? If so then your connection string should be just a regular connection string - something like this:
<add name="PrimaryDBConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=squirtprimary;Persist Security Info=True;Integrated Security=true;MultipleActiveResultSets=True;Application Name=EntityFramework" />
(You also don't have to do magic with configuration manager - you can just provide the name of the connection string to your ctor like this:
"name=PrimaryDBConnectionString"
and it should work)
On the other hand, if you are not using DbContext but ObjectContext (the exception you get would indicate this - you did not get an exception saying that your connection string is wrong or you are missing the providerName parameter). Then you would need to check if you are able to connect to the database without EF. A few hints:
- you use an IP address as your Data Source - if this is a remote server are you sure you enabled accepting external clients? (AFAIR this is disabled in Sql Server by default)
- you are using user/pwd for authentication - are you sure that these are correct
One of the ways to check the above is to open Sql Server Management Studio on your machine and provide the data you have in your connection string.
The exception you are seeing does not indicate any problems with metadata part. It is specifically about not being able to open the connection to the database.
来源:https://stackoverflow.com/questions/11132003/how-to-create-a-valid-connection-string-for-entity-framework-the-underlying-pr