I’m a new beginner to the entity framework .
I try to write te following method but i get a compile time error.
protected static void EntitySQLQuery(AWEntities context)
{
string cmd = @"SELECT VALUE c FROM AWEntities.Person AS c WHERE c.FirstName = @FirstName";
ObjectQuery<Person> persons = new ObjectQuery<Person>(cmd, context);//Error
}
The best overloaded method match for
‘System.Data.Objects.ObjectQuery.ObjectQuery(string,
System.Data.Objects.ObjectContext)’ has some invalid arguments
This is one of the more confusing points of Entity Framework. What you have to realize is that ObjectContext is the old way of doing EntityFramework, circa 2010. This was before DbContext (and code first). You can still get to the ObjectContext by casting your DbContext to IOjbectContextAdapter, like this:
Which would make your query look like this:
I do not know if Entity Structured Queries are promoted any more. I would consider using LInQ where possible.
If you are reading Julie Lerman’s books, I would highly recommend that you pick up all three. The first one is a huge tomb explaining all of the fundamentals (and is written around the ObjectContext way of doing things), the second two are more practical to today’s code-first/dbcontext world.