Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityValidationErrors插入空日期时间值_C#_.net_Entity - Fatal编程技术网

C# EntityValidationErrors插入空日期时间值

C# EntityValidationErrors插入空日期时间值,c#,.net,entity,C#,.net,Entity,我正在尝试将空值保存到MS SQL数据库的datetime列中。 每当抛出异常且dob属性设置为null时,db.savechanges就会抛出以下错误: 一个或多个实体的验证失败。看见 “EntityValidationErrors”属性以获取更多详细信息 它可能看起来多余,但会holterContents.dob=null作为DateTime改变 try { holterContents.dob = new DateTime(Convert.ToInt32(infopatlines[

我正在尝试将空值保存到MS SQL数据库的datetime列中。 每当抛出异常且dob属性设置为null时,db.savechanges就会抛出以下错误:

一个或多个实体的验证失败。看见 “EntityValidationErrors”属性以获取更多详细信息

它可能看起来多余,但会
holterContents.dob=null作为DateTime改变

try
{
    holterContents.dob = new DateTime(Convert.ToInt32(infopatlines[38]), Convert.ToInt32(infopatlines[39]),
    Convert.ToInt32(infopatlines[40]));
}

catch (Exception a)
{
    holterContents.dob = null;
    Log.Error(a.Message);
}
db.C_HOLTERCONTENTS.Add(holterContents);
db.savechanges();

它会赶上布洛克吗

如果它没有赶上街区检查我们的日期“01/01/0001 12:00:00 AM”。如果它是这样出现的,那么您需要将其设置为“null”

将您的保存块放入具有的Try块中

catch (DbEntityValidationException ex)
        {
            // Retrieve the error messages as a list of strings.
            var errorMessages = ex.EntityValidationErrors
                  .SelectMany(x => x.ValidationErrors)
                  .Select(x => x.ErrorMessage);
            // Join the list to a single string.
            var fullErrorMessage = string.Join("; ", errorMessages);
            // Combine the original exception message with the new one.
            var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
            // Throw a new DbEntityValidationException with the improved exception message.
            throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
        }

检查将出现什么类型的错误…

它将捕获块,我已将日志记录设置为日志表。。我可以看到它要么在catch时更新为Null,要么我可以使用new datetime(1995,1,1)将其更改为更合法的内容。我想我可能已经找到了罪魁祸首tho,holtercontents的另一个属性,它是一个包含100个尾随空格的字符串,试图插入nvarchar(50)列。我会测试并回复你,谢谢你的帮助