Converting a datetime2 data type into a datetime data type resulted in a value outside the range. The instruction has been completed

Asked

Viewed 312 times

0

I have a class, a client who has a record date.

public class ClienteViewModel: Pessoa
{
        [ScaffoldColumn(false)]
        public DateTime? DataCancelamento { get; set; }
        [ScaffoldColumn(false)]
        public DateTime DataCadastro { get; set; }
        [ScaffoldColumn(false)]
        public DateTime? DataUltimaAtualizacao { get; set; }
}

in my view, I am a field input with DateTime.Now, but I don’t send this field to the controller action, so the client.Dataregistration, comes 01/01/0001 00:00:00 I overwrote the method SaveChanges();

 public override int SaveChanges()
        {
            foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("DataCadastro") != null))
            {
                if (entry.State == EntityState.Added)
                {
                    entry.Property("DataCadastro").CurrentValue = DateTime.Now;
                }
                if (entry.State == EntityState.Modified)
                {
                    entry.Property("DataCadastro").IsModified = false;
                }
            }
            return base.SaveChanges();
        }

to fill my field dataCadastroas DateTime.Now; my controller is like this:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ClienteViewModel cliente)
        {


            if (ModelState.IsValid)
            {

                var clienteDomain = Mapper.Map<ClienteViewModel, Cliente>(cliente);
                if (clienteDomain.ClienteId == 0)
                {
                    _clienteApp.Adicionar(clienteDomain);
                }
                else {
                    _clienteApp.Atualizar(clienteDomain);
                }
                return RedirectToAction("Cliente", clienteDomain);
            }
            ViewBag.RamoAtividadeId = new SelectList(_ramoapp.BuscarTodos(), "RamoId", "Descricao");
            ViewBag.VendedorId = new SelectList(_vendedorAppservico.BuscarTodos(), "VendedorId", "Nome");
            ViewBag.TabelaDePrecoId = new SelectList(_tabelaDePrecoAppServico.BuscarTodos(), "TabelaDePrecoId", "Descricao");
            return RedirectToAction("Cliente", cliente);
        }

I already debugged the code and could not find the problem of that date. not saving you because every time the following message:

Conversion of a datetime2 data type into a datetime data type resulted in a value outside the range. The instruction has been completed.

I don’t know what’s causing this problem.

in my class ClienteConfiguracao, is like this:

Property(e => e.DataCadastro)
                .IsRequired()
                 .HasColumnType("datetime");

Would anyone know where I’m going wrong??

  • 2

    guy puts a breakpoint on his savechanges and sees if he’s actually picking up datetime.now, this mistake happens precisely because datetime has a smaller range than datetime2, so in thesis he wouldn’t contemplate the date 01/01/0001 00:00:00

  • I think the error, ta at the time of mapping var clienteDomain = Mapper.Map<ClienteViewModel, Cliente>(cliente);

  • the error was in my Client.ComplementPersonFisica. --> Clientviewmodel.ComplementPersonFisicaViewModel. in my domain, I had a field public Datatime DataDeNascimento {get;set;} and in my viewmodel was so-> public Datetime? Dating {get: set;}, so at the time of mapping, I think it was crazy,!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.