[英]Trying to follow the example for ToTraceString(), but it fails. Why?
I've got to find a way to find out what the SQL SELECT that is being generated when interacting with EF, is. 我必须找到一种方法来找出与EF交互时正在生成的SQL SELECT是什么。 I found the MSDN online help describing it ObjectQuery.ToTraceString Method() and I am certain that I've followed the example exactly, but it is failing. 我找到了描述它的MSDN联机帮助ObjectQuery.ToTraceString Method(),并且可以肯定我完全遵循了该示例,但是它失败了。 I am getting the following error: 我收到以下错误:
CS1503 Argument 2: cannot convert from 'string' to
'System.Linq.Expressions.Expression<System.Func<ResearchTableUpdates.Model.Template, bool>>'
I don't get why I'm getting this error. 我不明白为什么我会收到此错误。 Here's the code that generates the error: 这是生成错误的代码:
using (var db = new MyDbContext())
{
int idNum = 1;
ObjectQuery<Model.Template> oqTemplate = db.Templates.Where("it.ID = @idNum");
oqTemplate.Parameters.Add(new ObjectParameter("idNum", idNum));
Console.WriteLine(oqTemplate.ToTraceString());
The MyDbContext is a class I defined. MyDbContext是我定义的类。 Here is it's definition: 这是它的定义:
public class MyDbContext : Model.CoreFrameworkEntities
{
public override int SaveChanges()
{
var modifiedEntities = ChangeTracker.Entries()
.Where(p => p.State == EntityState.Modified)
.Select(p => p.Entity);
foreach (var modified in modifiedEntities)
{
Console.WriteLine();
//modified.LastModifiedAt = now;
Console.WriteLine(modified.ToString());
Console.WriteLine();
}
return base.SaveChanges();
}
}
And finally Model.CoreframeworkEntities
is an EF model defined creating a .edmx file. 最后, Model.CoreframeworkEntities
是一个定义为.edmx文件的EF模型。 It is one of the entities in the .edmx. 它是.edmx中的实体之一。 (It is my understanding that any entity defined in a .edmx file is a DbContext data type. Now, if that's wrong, I'd love to know.) (据我了解,.edmx文件中定义的任何实体都是DbContext数据类型。现在,如果那是错误的,我很想知道。)
I'm not sure why I'm getting the error that I am. 我不确定为什么会出现错误。 It seems to me as though I've followed the MSDN article in code. 在我看来,我似乎已经在代码中关注了MSDN文章。 Either the MSDN article is no longer valid or I've made a mistake, possibly in my understanding as to what the entities in a entity-relationship/.edmx file are all about. MSDN文章不再有效,或者我犯了一个错误,可能是在我理解了object-relationship / .edmx文件中的所有实体之后。
That's because that article uses context inherited from ObjectContext . 这是因为该文章使用了从ObjectContext继承的上下文。 You use context inherited from DbContext
. 您使用从DbContext
继承的上下文。 That old ObjectContext
has ObjectSets
which indeed has Where
overload which accepts string and returns ObjectQuery
. 那个旧的ObjectContext
有ObjectSets
,确实有Where
重载,它接受字符串并返回ObjectQuery
。 However DbContext
(which you use) has DbSets
and Where
methods on them do not accept strings, but only Expressions hence your error. 但是, DbContext
(您使用的)具有DbSets
并且Where
上的Where
方法不接受字符串,而仅接受表达式,因此会出现错误。 With DbContext
you usually either use interceptors, or Database.Log
property, like this: 使用DbContext
,通常可以使用拦截器或Database.Log
属性,如下所示:
db.Database.Log = Console.WriteLine; // or any other handler
var templates = db.Templates.Where(c => c.ID == idNum).ToArray(); // sql will be written to console.
As Gert Arnold mentions in comments, you can also call ToString() on query: 正如Gert Arnold在评论中提到的那样,您还可以在查询中调用ToString():
db.Templates.Where(c => c.ID == idNum).ToString()
However in some cases you cannot do that (for example you end your query with Count()
, or First()
and so on). 但是,在某些情况下,您无法执行此操作(例如,以Count()
或First()
等结束查询)。 But when you can do that, and you need only sql for a certain single query - of course ToString
method is better. 但是,当您可以做到这一点时,并且只需要对单个查询执行ToString
当然, ToString
方法更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.