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!