使用EF Database First的日期时间帖子上的ModelState.IsValid为false

时间:2017-04-18 14:16:32

标签: c# sql-server entity-framework-6 restsharp ef-database-first

我使用的是Microsoft的Entity Framework 6.1.3。使用Database First(来自Azure SQL Server实例)。自动生成的实体看起来像这样 - 数据库列是 datetime2(0)但实际上我只想保留日期,但这是另一个讨论:

public partial class Liquidated
{
    public long ID { get; set; }
    public System.DateTime LiquidationDate { get; set; }
    public string Comment { get; set; }
}

自动生成的控制器:

    // POST: api/Liquidations
    [ResponseType(typeof(Liquidated))]
    public IHttpActionResult PostLiquidated(Liquidated liquidated)
    {

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

使用RestSharp的单元测试:

        var client = new RestClient("http://localhost:64185/api/Liquidations");
        var request = new RestRequest(Method.POST);

        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("authorization", "bearer " + token.access_token);
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        request.AddHeader("audience", "Any");
        request.AddHeader("accept-language", "en-gb");
        request.AddHeader("accept", "application/json");

        var liquidation = new Liquidated();

        liquidation.LiquidationDate = DateTime.Now;
        liquidation.Comments = "Updated group name 1";

        request.AddObject(liquidation);

        IRestResponse<Liquidated> response = client.Execute<Liquidated>(request);

但是当我在Controller中检查ModelState时它无效并且为LiquidationDate显示以下格式错误:

"The value '18/04/2017 14:26:12' is not valid for LiquidationDate."

我搜索了很多帖子,试图强制格式化(这是C#实体和SQL Server之间的日期格式化问题吗?)但是我无法看到我做错了什么。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

如果将JSON数据发布到Web API,请确保以ISO 8601格式(2017-04-18T14:22:59Z)发送日期值。否则你会遇到模型绑定器的问题。

将UTC Javascript日期对象转换为ISO 8601字符串的JS函数示例(仅限日期没有时间):

/// <summary>
/// Takes a utc js date and converts it to a iso8601 utc string
/// </summary>
GlobalJsHelpers.Iso8601FromUtc = function(utcDate) {
    //create a two digit month string (01-12)
    var month = ('0' + (utcDate.getUTCMonth() + 1)).substr(-2);
    //create a two digit day string (01-31)
    var day = ('0' + utcDate.getUTCDate()).substr(-2);
    return utcDate.getUTCFullYear() + '-' + month + '-' + day;
};

请参阅https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service了解如何欺骗RestSharp。

答案 1 :(得分:0)

继@ DavidG的评论后,我将客户端Liquidated类更改为包含字符串而不是datetime:

public partial class Liquidated
{
    public long ID { get; set; }
    public string LiquidationDate { get; set; }
    public string Comments { get; set; }
}

然后在RestSharp之外进行格式化:

        var client = new RestClient("http://localhost:64185/api/Liquidations");
        var request = new RestRequest(Method.POST);
        request.AddHeader("postman-token", "a95ce9b5-8c7f-e223-faa6-3768f7edd10c");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("authorization", "bearer " + token.access_token);
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        request.AddHeader("audience", "Any");
        request.AddHeader("accept-language", "en-gb");
        request.AddHeader("accept", "application/json");

        var liquidation = new Liquidated();

        liquidation.LiquidationDate = DateTime.Now.ToString("yyyy-MM-dd");
        liquidation.Comments = "Updated group name 1";

        // request.DateFormat = "yyyy-MM-dd"; // This is ignored by RestSharp
        request.AddObject(liquidation);

        IRestResponse<Liquidated> response = client.Execute<Liquidated>(request);

阅读RestSharp GitHub问题日志:https://github.com/restsharp/RestSharp/issues/629后,没有计划支持此功能。

感谢您指出我正确的方向。