Search code examples
c#sql-serverentity-framework-6restsharpef-database-first

ModelState.IsValid is false on datetime post with EF Database First


I am using Microsoft's Entity Framework 6.1.3. with Database First (from an Azure SQL Server instance). The automatically generated Entity looks like this - the database column is datetime2(0) but really I just want to persist the date but that's another discussion:

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

The auto-generated Controller:

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

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

The unit test using 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);

But when I inspect the ModelState in the Controller it is not valid and shows the following format error for LiquidationDate:

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

I've searched many posts, tried to force the formatting (is it a question of date formatting between C# entities and SQL Server?) but I can't see what I'm doing wrong. Any help is much appreciated!


Solution

  • Following on from @DavidG's comment I changed the client-side Liquidated class to contain a string instead of datetime:

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

    and then did the formatting outside of 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);
    

    Having read the RestSharp GitHub issue log: https://github.com/restsharp/RestSharp/issues/629, there are no plans to support this facility.

    Thanks for pointing me in the right direction.