简体   繁体   English

尝试按照ToTraceString()的示例进行操作,但失败。 为什么?

[英]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 . 那个旧的ObjectContextObjectSets ,确实有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.

相关问题 WPF MultiBinding失败。 为什么? - WPF MultiBinding Fails. Why? Web部署到IIS失败。 为什么? - Web Deploy to IIS fails. Why? 尝试捕获FormatException错误失败。 只有Exception可以捕获吗? - Trying to catch a FormatException error fails. Only Exception can catch it? 在web.config中注册自定义控件失败。 为什么? - Registering custom control in web.config fails. Why? 尝试学习Azure但示例项目失败。 如何获得它来构建/运行? - Trying to learn Azure but sample project fails. How to get it to build/run? 一种方法成功切换了控件的“ Enabled”属性-一种类似的方法失败了。 为什么? - One method successfully toggles a control's “Enabled” property - a similar method fails. Why? 通过反射设置winform面板名称时,将visible设置为true失败。 为什么? - When setting winform panel name by reflection, setting visible to true fails. Why? InitiateSystemShutdown失败。 错误代码53 - InitiateSystemShutdown fails. Error code 53 来自 JavaScript 的 WebMethod 调用失败。 - WebMethod call from JavaScript fails. JsonConvert.DeserializeObject("MyString",typeof(string)) 失败。 这甚至可能吗? - JsonConvert.DeserializeObject("MyString",typeof(string)) fails. Is this even possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM