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 ?!!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
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;
}
}
Method 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
Method 3
I am using ef core and I want to get server time from sqlserver and none of the approaches worked in my case. IObjectContextAdapter is not recognized, it asked me to download ef 6.
I have found below statement to get server time:
private DateTime GetServerTime()
{
using (var db = new DbContext())
{
DbConnection connection = db.Database.GetDbConnection();
connection.Open();
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT GETDATE()";
return (DateTime)cmd.ExecuteScalar();
}
}
You can use this method to create any sql statement and get data from db.
Hope it helps somebody.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0