我使用EF 6.0并在context.SaveChanges()上收到此错误(非可空成员的空值。成员:'详细信息')。该值不为null,但EF仍在抱怨它是:
这适用于两个“CrashDump”对象。
为什么会这样?
编辑:
以下是所有相关代码:
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
{
}
答案 0 :(得分:2)
这是合理的,因为在非可空字段中,您尝试保存空值。你可以解决这个问题,让你在数据库中的字段可以为空,然后更新你的模型。
答案 1 :(得分:0)
问题可能正在发生,因为您使用dump.Details
设置FirstOrDefault()
的值,当结果集合中不包含任何元素时会导致null
。
检查该方案并为其指定一个值,以确保不会发生。