Can't find CreateQuery() method

北城余情 提交于 2019-11-29 17:00:46

问题


I'm a new beginner to the entity framework .

and i can't find the following method CreateQuery()


why i can't find this method ?!!


回答1:


Since ESQL was considered an advanced use case, there is no straightforward API from DbContext. You can access the ObjectContext that backs your DbContext to do what you want:

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

Related: http://thedatafarm.com/blog/data-access/accessing-objectcontext-features-from-ef-4-1-dbcontext/

As suggested there, you can also add a method ( or property) ObjectContext to your context class:

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

    public ObjectContext ObjectContext()
    {
        return (this as IObjectContextAdapter).ObjectContext;
    }
}



回答2:


First of all, this is not the native solution for querying in EF. Please learn as much LINQ as you can and then, if you know you really need alternate methods, fall back to CreateQuery()

But, you can get the result you want with casting to System.Data.Entity.Core.Objects.IObjectContextAdapter like this:

(context as IObjectContextAdapter).ObjectContext.CreateQuery

Also, you can run sql commands with:

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

Hope this helps



来源:https://stackoverflow.com/questions/14506161/cant-find-createquery-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!