0

I'm trying to use Entity Framework but I keep getting the error "The underlying provider failed on Open.". I am storing the password within the code at the getConnectionString function provides a valid connection string and I know it works because I use that function throughout (albeit throughout my non-Entity Framework programs). I have Customer defined in my Model1 file. Customer is a database.

// gets valid connection string
string cs = getConnectionString();

var entityConnectionStringBuilder = new EntityConnectionStringBuilder();
entityConnectionStringBuilder.Provider = "System.Data.SqlClient";
entityConnectionStringBuilder.ProviderConnectionString = cs;
entityConnectionStringBuilder.Metadata = "res://*";


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

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

I am new to Entity Framework and am trying to make a simple test app to help me understand it. Any help would be greatly appreciated. I have rebuilt the project from scratch to make sure that I had all the parts I needed for EF.

Missy
  • 1,286
  • 23
  • 52
  • 1
    Is 'Customer' an IEnumerable type? Here's an example: https://stackoverflow.com/questions/8215773/could-not-find-an-implementation-of-the-query-pattern – Matthew Alltop May 22 '17 at 16:28
  • I saw that post and tried to model after it without success – Missy May 22 '17 at 16:42
  • 1
    Come to think of it, you should be careful with the latest edit. It changes the question and should probably be a new one now (in which you might want to provide your current `CUSTEntities1` – Brunner May 22 '17 at 19:00

1 Answers1

1

You should be querying from the context, EF doesn't know what to do with the class alone.

The query would need to start with

from customer in context.Customers

Since it seems that you are using a model- or database-first approach, the context should already contain a DbSet. In your case it's in CUSTEntities1.

Brunner
  • 1,945
  • 23
  • 26