*Update* It turns out that I completely misunderstood Zlatko Michailov’s email reply to me when I was having trouble with this. I seem to have freaked out the team by writing this errant blog post. Sorry guys.
When using ObjectQuery, I was doing this:
Dim myAWEntities=New AdventureWorksEntities
dim myObjQuery=myAWEntities.CreateQuery(Of Customer)(“SELECT VALUE cust from myAWEntities.Customer as cust “)
I was thinking in LINQ, using the variable of the instance I had created.
But if I use the entitySet name, it’s fine.
“SELECT VALUE cust from AWEntities.Customer as cust ” &
I am having a really hard time believing that I didn’t try that since I was trying a lot of things, but it is now working and I’m still on Beta2.
I went around in circles with this one a few weeks ago (preparing for a session at DevConnections) and saw that someone had the same problem on the forums yesterday so thought I would blog it.
I have no idea if it will change going forward, but this is in E.F. Beta 2.
Entity SQL is the query language that you use when
1. querying through ObjectServices directly by creating an ObjectQuery as in
Dim MyObjQuery=MyObjectContext.CreateQuery(Of MyEntityClassType)(myEntitySQLQueryString).
or 2. querying through the Provider: EntityClient
Dim myCommand=New EntityCommand(myEntitySqlQueryString,myEntityConnection)Dim myDataReader=myCommand.ExecuteReader(COmmandBehavior.SequentialAccess)
A simplistic EntitySQL query looks something like this:
“Select cust from Customer as cust” where Customer is referring to an EntityClass in my model.
The difference between using this with ObjectServices vs. EntityCLient is that in ObjectServices, you will have already identified the namespace that the CustomerClass lives in when you create the ObjectContext.
FOr example,
Dim myAWEntities=New AdventureWorksEntities
So when you create the ObjectQuery, it actually barfs, I mean throws an exception, if you tell it the namespace again. So you just want plain old Customers.
If my query read “Select cust from AdventureWorksEntities.Customer as cust”
The exception would be
Failed to resolve AdventureWorksEntities.Customer’ in the current scope or context. Make sure referenced variables are in scope, required schemas are loaded and namespaces referenced correctly, near multipart identifier, line xxx, number xxx.
Now with EntityClient, there is no indication outside of the query string as to the namespace, so you NEED to tell it the namespace.
Unfortunately, it’s not obvious because if you look at examples out on the web you’ll see it both ways and it might take a while (as it did in my case) for the subtle difference to become clear.
Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!
Ironically enough I actually just noticed this tidbit the other day as well. Good to see someone providing some clarification on the matter 🙂