简体   繁体   中英

Entity Framework “Could not find an implementation of the query pattern”

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM