Asp.net实体框架的datetime问题 [英] Asp.net entity framework datetime problem

查看:62
本文介绍了Asp.net实体框架的datetime问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



i想为mysql数据库中的用户写生日。

i为我的mysql服务器创建实体框架。

然而,

当我尝试添加生日时我得到了



类型'System.Data.Entity.Validation的例外.DbEntityValidationException'发生在TarifinizBizde.dll中但未在用户代码中处理



附加信息:一个或多个实体的验证失败。有关更多详细信息,请参阅'EntityValidationErrors'属性。



此异常。



我认为mysql datetime格式为:yyyy-mm-dd

和实体框架日期时间格式为:dd-mm-yyyy



如何我可以更改mysql或实体框架的日期时间吗?



Hello

i want to write birthday for users in mysql database.
i create entity framework to my mysql server.
However,
when i try to add birthday i got

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in TarifinizBizde.dll but was not handled in user code

Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

this exception.

and i think the mysql datetime format is:"yyyy-mm-dd"
and entity framework date time format is:"dd-mm-yyyy"

how can i change mysql or entity framework datetime?

usr.DogumTarihi = Convert.ToDateTime(txtDogum.Text);





我尝试了什么:



使用全球化tostring (yyyy-mm-dd)

但他们没有工作



What I have tried:

using globalization tostring("yyyy-mm-dd")
but they are not worked

推荐答案





你可以尝试如下:



Hi,

Can you try it like below:

bool parsed = false;
DateTime outDate;
parsed = DateTime.TryParseExact(inputDate, "dd-MM-yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out outDate);

if(parsed)
{
    usr.DogumTarihi = outDate.ToString("yyyy-MM-dd");
}
else
{
    // Invalid date has been passed
}





但请记住你会需要使用System.Globalization命名空间来使用它。



请告诉我。



谢谢。



But remember you will need to use System.Globalization namespace to use this.

Please let me know.

Thanks.


首先,找出你得到的错误。然后回来看看结果。这段代码可能会帮助您找出错误:



First, find out what are the errors you are getting. Then come back with the result. This piece of code might help you finding out the errors:

try
{
    // put your code here for saving or updating record.
}
catch (DbEntityValidationException ex)
{
StringBuilder sbError = new StringBuilder();
    foreach (var objError in ex.EntityValidationErrors)
    {
sbError.AppendFormat("Entity Type \"{0}\" in state \"{1}\" has the following errors:", objError.Entry.Entity.GetType().Name, objError.Entry.State);
        foreach (var validationError in objError.ValidationErrors)
        {
sbError.AppendFormat("Property: \"{0}\", Error: \"{1}\"",
                validationError.PropertyName, validationError.ErrorMessage);           
        }
    }
string validationErrors = sbError.ToString();
    //put you debugger here and see what is the value of "validationErrors"
}


这篇关于Asp.net实体框架的datetime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆