2

I know that a lot of people think it is a bad idea to hard code connection info but I have a specific situation where I need to do it. Please don't downtick me because you think this is a bad idea - again, very specific circumstance.

Using the code below, I get the following error on the LINQ statement: The underlying provider failed on Open. I have tested the connection string independently and I have no trouble connecting with it.

public partial class Form1 : Form
{
    public Form1()
    {
       InitializeComponent(); 

        var entityConnectionStringBuilder = new EntityConnectionStringBuilder();
        entityConnectionStringBuilder.Provider = "System.Data.SqlClient";
        entityConnectionStringBuilder.ProviderConnectionString = "Data Source=DESKTOP-A43456\\MAIN;Initial Catalog=MAIN;Persist Security Info=True;User ID=AppUser;Password=3092409SALFKJ93LK342";
        entityConnectionStringBuilder.Metadata = "res://*";


        MyEntities CustContext = new MyEntities(entityConnectionStringBuilder.ToString());

        var q = (from i in CustContext.Customers
                 where i.fid3 == 15
                 select new CustClass
                 {
                     fid1 = i.fid1,
                     fid2 = i.fid2,
                     fid3  = (int)i.fid3
                 }).ToList<CustClass>();

    }
} 

Thank you in advance for any help and insight you can provide. I did look at other relevant posts and tried various changes on Metadata but they did not work.

9
  • 3
    Read the InnerException. Commented May 22, 2017 at 21:56
  • You need Multiple Active Result Sets Commented May 22, 2017 at 21:56
  • Could you show us the constructor of MyEntities? Commented May 22, 2017 at 22:29
  • 1
    Please tell me you didn't just post the actual credentials in your connection string. At least replace them with something generic to get the point across like "someuser" and "somepassword". Commented May 22, 2017 at 22:44
  • @Brunner public partial class Entities : DbContext { public Entities(string nameOrConnectionString) : base("name=Entities") { } Commented May 22, 2017 at 23:38

1 Answer 1

1

Given the class definition you've shared in the comments, your problem lies with the Entities class.

Its constructor should pass the connection string to the DbContext base class constructor:

public Entities(string nameOrConnectionString) : base(nameOrConnectionString) {}

All the best with your project!

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.