Can't find CreateQuery() method

asp.net c# createquery entity-framework visual-studio-2012

Question

I'm just getting started with the entity framework.

and I can't seem to discover the CreateQuery() technique.


enter image description here

Why can't I discover this technique?

1
15
1/24/2013 4:32:39 PM

Accepted Answer

There is no simple API from since ESQL was seen as an advanced use case.DbContext You can reach theObjectContext it supports yourDbContext to act as you please:

((IObjectContextAdapter)context).ObjectContext.CreateQuery<Person>("esql..")

Associated: 20-zzz

There was a suggestion to include a method, too ( or property)ObjectContext to your class of context:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    public ObjectContext ObjectContext()
    {
        return (this as IObjectContextAdapter).ObjectContext;
    }
}
20
2/3/2013 12:41:36 PM

Popular Answer

First off, this is not EF's default method of querying. Please learn as much LINQ as you can before switching to CreateQuery if you know you absolutely need other options ()

However, casting to the desired outcome is a viable option.System.Data.Entity.Core.Objects.IObjectContextAdapter as in this:

(context as IObjectContextAdapter).ObjectContext.CreateQuery

You can execute SQL instructions using:

context.Database.SqlQuery<>() and context.Database.ExecuteSqlCommand()

Hope this is useful.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow