简体   繁体   English

LinqPad实体框架和NullExceptionReference

[英]LinqPad Entity Framework and the NullExceptionReference

this is my first post here and I'm also new to C# / OO and LinqPAD. 这是我的第一篇文章,也是C#/ OO和LinqPAD的新手。 (translate to: I may mis-use concepts or terms due to being in 'learning mode') (转换为:由于处于“学习模式”,我可能会误用概念或术语)

I have an application that I've narrowed my bug down to the following query. 我有一个应用程序,已将错误范围缩小到以下查询。 I decided to try out LinqPAD to solve the issue in a constrained environment. 我决定尝试使用LinqPAD在受限环境中解决此问题。

We are using Entity Framework for our database interactions. 我们正在使用实体框架进行数据库交互。 I am using LinqPAD's "C# Program" selection. 我正在使用LinqPAD的“ C#程序”选择。

I found a great tutorial on how to use entities in LinqPAD and this seems to be working. 我找到了一个有关如何在LinqPAD中使用实体的出色教程,这似乎正在起作用。 (Proof: 'aEntities' does not return an error like it did when the entities were not connected properly) (证明:“ aEntities”不会像实体未正确连接时那样返回错误)

So let's get to the issue. 因此,让我们解决这个问题。

At the 'var' I get: "NullReferenceException: Object reference not set to an instance of an object." “ var”处,我得到:“ NullReferenceException:对象引用未设置为对象的实例。”

The reason I'm here asking this is every single example I see out there uses this 'var' approach. 我在这里问这个问题的原因是,我看到的每个示例都使用这种“ var”方法。 It does not work for some reason. 由于某种原因,它不起作用。 I've tried declaring 'qryDC', creating a new instance of it and then assigning it. 我尝试声明“ qryDC”,创建它的新实例,然后分配它。 (I'm feeling weak knowledge wise here, but am reading and learning.) (我在这里感到知识薄弱,但是正在阅读和学习。)

I have read John Saunders epic write up about this topic. 我已经阅读了约翰·桑德斯(John Saunders)的史诗般的有关该主题的文章。 (Wow man! Thanks!) What is a NullReferenceException, and how do I fix it? (哇!谢谢!) 什么是NullReferenceException,我该如何解决?

I however was not able to translate that knowledge to address my issue (learning curve). 但是,我无法翻译这些知识来解决我的问题(学习曲线)。

Here is the code: 这是代码:

void Main(AEntities aEntities)
{

  decimal fmProjectUID = 1123;

  var qryDC = 
        from pnp in aEntities.PNonPRs 
            from p in aEntities.Projects
            from pt in aEntities.PurchaseTypes
            from wbs in aEntities.P32
            from pc in aEntities.PPhases
            where pnp.ProjectUID == p.ProjectUID
            where p.ProjectUID == fmProjectUID
            where pnp.PurchaseTypeUID == pt.PurchaseTypeUID
            where pnp.P32UID == wbs.P32UID
            where pt.IsNonPR == 1
            orderby pt.PurchaseTypeID
            select new 
            {
                PNonPrUID = pnp.PNonPrUID
                ,PurchaseTypeID = pt.PurchaseTypeID
                ,PhaseCode = pnp.ProjectPhase.PhaseCode
                ,DANbr = pnp.AType.DANbr
                ,PreAmt = pnp.PreAmt
                ,POStDate = pnp.POStDate
                ,POFDate = pnp.POFDate
                ,BaseAmt = pnp.BaseAmt
                ,Notes = pnp.Notes
        ,ChangedByName = pnp.ChangedByName
            };

 }

Let me know if you need more info. 让我知道您是否需要更多信息。

And thank you for your time, effort and thoughts. 并感谢您的时间,精力和想法。

You can't just arbitrarily assign arguments to the Main method and expect them to be filled in. Your aEntities variable is null when you run this, thus the null reference exception. 您不能只是将参数任意分配给Main方法并期望它们被填充。运行此方法时, aEntities变量为null,因此为null引用异常。 You need to instantiate your context. 您需要实例化上下文。

void Main()
{
    decimal fmProjectUID = 1123;

    var aEntities = new AEntities();
    var qryDC = from pnp in aEntities.PNonPRs 
                from p in aEntities.Projects
                from pt in aEntities.PurchaseTypes
                // etc...
}

Alternatively, if you defined your connection in LINQPad and then selected said connection for the query you can omit the creation of the AEntities and just use the following. 或者,如果您在LINQPad中定义了连接,然后为查询选择了所述连接,则可以省略AEntities的创建,而仅使用以下内容。

void Main()
{
    decimal fmProjectUID = 1123;

    var qryDC = from pnp in PNonPRs 
                from p in Projects
                from pt in PurchaseTypes
                // etc...
}

I would also suggest you familiarize yourself with the debugger in Visual Studio and LINQPad. 我还建议您熟悉Visual Studio和LINQPad中的调试器。 Had you set a breakpoint and examined the variables you would have seen that aEntities was null. 如果您设置了一个断点并检查了变量,您会发现aEntities为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM