简体   繁体   中英

Why am I receiving a “Null value for non-nullable member” error?

I utilizing EF 6.0 and receiving this error (Null value for non-nullable member. Member: 'Details') on context.SaveChanges(). The value is NOT null but EF is still complaining that it is:

非空错误

This applies for both of the "CrashDump" objects.

Why would this be happening?

EDIT:

Here is all relevant code:

try
{
    if (Uri.CheckHostName(computer) != UriHostNameType.Unknown)
    {
        Ping pingSender = new Ping();
        PingOptions pingOptions = new PingOptions();

        pingOptions.DontFragment = true;
        byte[] buffer = Encoding.ASCII.GetBytes("abcde");
        int timeout = 1;
        PingReply pingReply = pingSender.Send(computer, timeout, buffer, pingOptions);

        string uncPath = String.Format("\\\\{0}\\c$\\windows\\minidump", computer);

        if (pingReply.Status == IPStatus.Success)
        {
            Computer computerObj = (from c in context.Computers
                                    where c.Name == computer
                                    select c).First();
            List<CrashDump> dumps = computerObj.Dumps;

            Process bsodViewProcess = new Process();
            bsodViewProcess.StartInfo.UseShellExecute = false;
            bsodViewProcess.StartInfo.Arguments = 
                string.Format("/loadfrom 1 /minidumpfolder \\\\{0}\\c$\\windows\\minidump /sxml dumps.xml", computer);
            bsodViewProcess.StartInfo.FileName = "BlueScreenView.exe";

            bsodViewProcess.Start();
            bsodViewProcess.WaitForExit();

            XDocument xDoc = XDocument.Load("dumps.xml");

            List<CrashDumpDetails> lstDumpDetails =
                (from dump in xDoc.Element("crash_list").Elements("item")
                    select new CrashDumpDetails
                    {
                        DumpFile = dump.Element("dump_file").Value,
                        CrashTime = dump.Element("crash_time").Value,
                        BugCheckString = dump.Element("bug_check_string").Value,
                        BugCheckCode = dump.Element("bug_check_code").Value,
                        Parameter1 = dump.Element("parameter_1").Value,
                        Parameter2 = dump.Element("parameter_2").Value,
                        Parameter3 = dump.Element("parameter_3").Value,
                        Parameter4 = dump.Element("parameter_4").Value,
                        CausedByDriver = dump.Element("caused_by_driver").Value,
                        CausedByAddress = dump.Element("caused_by_address").Value,
                        FileDescription = dump.Element("file_description").Value,
                        ProductName = dump.Element("product_name").Value,
                        Company = dump.Element("company").Value,
                        FileVersion = dump.Element("file_version").Value,
                        Processor = dump.Element("processor").Value,
                        CrashAddress = dump.Element("crash_address").Value,
                        StackAddress1 = dump.Element("stack_address_1").Value,
                        StackAddress2 = dump.Element("stack_address_2").Value,
                        StackAddress3 = dump.Element("stack_address_3").Value,
                        ComputerName = dump.Element("computer_name").Value,
                        FullPath = dump.Element("full_path").Value,
                        ProcessorsCount = dump.Element("processors_count").Value,
                        MajorVersion = dump.Element("major_version").Value,
                        MinorVersion = dump.Element("minor_version").Value,
                        DumpFileSize = dump.Element("dump_file_size").Value,
                        DumpFileTime = dump.Element("dump_file_time").Value
                    }).ToList();

            foreach (var file in Directory.GetFiles(String.Format("\\\\{0}\\c$\\windows\\minidump", computer)))
            {
                Console.WriteLine(String.Format("\t{0}", file));

                string fileName = Path.GetFileName(file);
                DateTime lastWriteTime = File.GetLastWriteTime(file);

                var existingDump = from d in dumps
                                    where d.FileName == Path.GetFileName(file)
                                    where d.Time.Truncate(TimeSpan.FromSeconds(1)) 
                                            == File.GetLastWriteTime(file).Truncate(TimeSpan.FromSeconds(1))
                                    select d;

                if (existingDump.Count() == 0)
                {
                    CrashDump dump = new CrashDump();
                    dump.FileName = Path.GetFileName(file);
                    dump.Time = File.GetLastWriteTime(file);

                    if (lstDumpDetails.Count > 0)
                    {
                        dump.Details = lstDumpDetails
                            .Where(d => Path.GetFileName(d.DumpFile) == dump.FileName)
                            .FirstOrDefault();
                    }

                    dumps.Add(dump);
                }
            }

            computerObj.Dumps = dumps;

            context.SaveChanges();
        }
    }
}
catch
{

}

This is reasonable because in a non nullable field you try to save a null value. You could fix this, making you field in the database to be nullable and then update your model.

The issue could be occurring because you're setting the value of dump.Details using FirstOrDefault() which results in null when the resulting collection contains no elements.

Check for that scenario and assign it a value to ensure that doesn't occur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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