第一次成功调用后,LINQ 查询继续返回 null

cra*_*020 0 c# linq asp.net asp.net-mvc

我的 ASP.NET MVC Web 应用程序中有一个上传功能,它允许用户上传文档。系统然后使用各种 OCR API 从上传的文档中读取数据,然后将这些数据保存在数据库中的“SecondarySchoolSurvey”表中。我在上传代码中有一个 LINQ 查询,它在数据库“SecondarySchoolSurvey”表中找到与传入的 Id 匹配的行。然后更新该行中的各个字段并将其保存回数据库。

现在这个上传功能第一次运行正常。虽然如果我再次尝试使用上传功能(不重新启动 IIS 服务器),则会引发以下错误Object reference not set to an instance of an object.

设置各种断点后,我注意到 LINQ 查询:var s1 = db.SecondarySchoolSurveys.FirstOrDefault(s => s.Id == id);第二次调用它时返回 null,即它没有找到与传入的 Id 匹配的记录,即使它存在于数据库中。

所以现在,每次我想测试上传功能后,我都需要重置Web应用程序的IIS服务器。否则这个 LINQ 查询会null在我第二次尝试上传文档时返回。有没有人见过这样的事情?我不确定它提供代码有多大用处,但这是其中一个类,包含 LINQ 查询的类:

public class SurveyCheckboxAnswers
{
    private RDSContext db = new RDSContext();

    //Adds Question 2 answer
    public void AddQ2Answer(SurveyCheckboxCollections checkboxes, int id)
    {
        //find Survey record in db which matches id in order to update with checkbox data
        var s1 = db.SecondarySchoolSurveys.FirstOrDefault(s => s.Id == id);

        CheckboxData q2Male = checkboxes.SecondarySchoolCheckboxes["Q2Male"];
        CheckboxData q2Female = checkboxes.SecondarySchoolCheckboxes["Q2Female"];
        CheckboxData q2Other = checkboxes.SecondarySchoolCheckboxes["Q2Other"];
        CheckboxData q2DontWantToSay = checkboxes.SecondarySchoolCheckboxes["Q2DontWantToSay"];

        //numbers of checkboxes marked for validation
        int checkboxValidaiton = 0;

        //update SecondarySchoolSurvey checkbox answers in database with IsChecked values from checkbox dictionary 
        if (q2Male.IsChecked)
        {
            s1.Q2 = Gender.Male;
            checkboxValidaiton++;
        }
        if (q2Female.IsChecked)
        {
            s1.Q2 = Gender.Female;
            checkboxValidaiton++;
        }
        if (q2Other.IsChecked)
        {
            s1.Q2 = Gender.Other;
            checkboxValidaiton++;
        }
        if (q2DontWantToSay.IsChecked)
        {
            s1.Q2 = Gender.None;
            checkboxValidaiton++;
        }

        //validate only 1 checkbox has been marked 
        if(checkboxValidaiton == 0)
        {
            s1.Flag = true;
            s1.FlagContent += "| Question2: no checkboxes marked. ";
        }
        else if (checkboxValidaiton > 1)
        {
            s1.Flag = true;
            s1.FlagContent += "| Question2: more than 1 checkboxes marked. ";
        }

        db.SaveChanges();

    }

    //Adds Question 6 answer
    public void AddQ6Answer(SurveyCheckboxCollections checkboxes, int id)
    {
        //find Survey record in db which matches id in order to update with checkbox data
        var s1 = db.SecondarySchoolSurveys.FirstOrDefault(s => s.Id == id);

        CheckboxData q6Higher = checkboxes.SecondarySchoolCheckboxes["Q6Higher"];
        CheckboxData q6Ordinary = checkboxes.SecondarySchoolCheckboxes["Q6Ordinary"];
        CheckboxData q6Other = checkboxes.SecondarySchoolCheckboxes["Q6Other"];


        //numbers of checkboxes marked for validation
        int checkboxValidaiton = 0;     

        if (q6Higher.IsChecked)
        {
            s1.Q6a = MathLevel.Higher;
            checkboxValidaiton++;
        }
        if (q6Ordinary.IsChecked)
        {
            s1.Q6a = MathLevel.Ordinary;
            checkboxValidaiton++;
        }
        if (q6Other.IsChecked)
        {
            s1.Q6a = MathLevel.Other;
            checkboxValidaiton++;
        }

        //validate only 1 checkbox has been marked 
        if (checkboxValidaiton == 0)
        {
            s1.Flag = true;
            s1.FlagContent += "| Question6: no checkboxes marked. ";
        }
        else if (checkboxValidaiton > 1)
        {
            s1.Flag = true;
            s1.FlagContent += "| Question6: more than 1 checkboxes marked. ";
        }

        db.SaveChanges();
    }

    //Adds Question 7 answer
    public void AddQ7Answer(SurveyCheckboxCollections checkboxes, int id)
    {
        //find Survey record in db which matches id in order to update with checkbox data
        var s1 = db.SecondarySchoolSurveys.FirstOrDefault(s => s.Id == id);

        CheckboxData q7Physics = checkboxes.SecondarySchoolCheckboxes["Q7Physics"];
        CheckboxData q7Biology = checkboxes.SecondarySchoolCheckboxes["Q7Biology"];
        CheckboxData q7Chemistry = checkboxes.SecondarySchoolCheckboxes["Q7Chemistry"];
        CheckboxData q7Science = checkboxes.SecondarySchoolCheckboxes["Q7Science"];
        CheckboxData q7None = checkboxes.SecondarySchoolCheckboxes["Q7None"];

        //numbers of checkboxes marked for validation
        int checkboxValidaiton = 0;

        if (q7Physics.IsChecked)
        {
            s1.Q7 = "Physics";
            checkboxValidaiton++;
        }
        if (q7Biology.IsChecked)
        {
            s1.Q7 += "Biology";
            checkboxValidaiton++;
        }
        if (q7Chemistry.IsChecked)
        {
            s1.Q7 += "Chemistry";
            checkboxValidaiton++;
        }
        if (q7Science.IsChecked)
        {
            s1.Q7 += "Science Junior";
            checkboxValidaiton++;
        }
        if (q7None.IsChecked)
        {
            s1.Q7 += "None";
            checkboxValidaiton++;
        }

        //validate only 1 checkbox has been marked 
        if (checkboxValidaiton == 0)
        {
            s1.Flag = true;
            s1.FlagContent += "| Question7: no checkboxes marked. ";
        }

        db.SaveChanges();
    }
...
...
...
}
Run Code Online (Sandbox Code Playgroud)

Sel*_*ien 5

您在这里的问题很可能在于您正在为 db 上下文使用私有变量。使用上下文打开/关闭与数据库的连接的适当方法是使用 using 语句,如下所示:

using(var db = new RDSContext())
{
    //...Do work here with your context
}
Run Code Online (Sandbox Code Playgroud)

MSDN

上下文的生命周期在创建实例时开始,并在实例被释放或垃圾收集时结束。如果您希望将上下文控制的所有资源都放在块的末尾,请使用 using。使用 using 时,编译器会自动创建一个 try/finally 块并在 finally 块中调用 dispose 。