I created a simple CRUD using the ASP.NET MVC and I can save my records normally. If I looked in the database, my date ( DataCriation ) was stored like this:
2017-06-01 00: 01: 23,750
But when I try to edit the same record with an asynchronous method created by MVC Scaffolding, all fields in the table are retrieved, but the DataCriation appears to be null: p>
01/01/2000 00:00:00
Then (obviously) I get an error:
Converting a datetime2 data type to a data type datetime resulted in a value out of reach.
This is the generated method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ArtigoId,Titulo,Texto,DataPublicacao,UltimaModificacao,UsuarioModificacao,DataCriacao,UsuarioCriacao")] Artigo artigo)
{
if (ModelState.IsValid)
{
db.Entry(artigo).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(artigo);
}
After the problem I did some research and changed the field in the database to datetime2(0-7)
in EF Code First doing this in my model:
[DisplayName("Data de Criação")]
[Column(TypeName = "datetime2")]
public DateTime DataCriacao { get; set; }
This did not correct the problem, just made the field accept the 01/01/2000 00:00:00 date.
How do I retrieve the date correctly? Should I use datetime or datetime2?