2

Given the following connection string:

<add name="PrimaryDBConnectionString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=10.1.1.101;Initial Catalog=primary;Persist Security Info=True;User ID=myuserid;Password=mypassword;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" />

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=&quot;data source=10.99.108.42;initial catalog=primary;user id=dbuserID;password=somepw;multipleactiveresultsets=True;App=EntityFramework&quot;" 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 sure that provider connection string is correct? This error should not be related to metadata files but to the real connection string. Commented Jun 21, 2012 at 6:14

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.