none
DefaultValue in Entity Framework Core does not work RRS feed

  • Question

  • ​I am having a problem when generating a default value with Entity Framework Core, it is supposed to generate a `getdate()` value but it does not work. This is what I have in my fluent Api


    modelBuilder.Entity<Loteria>()
                    .Property(p => p.FechaCreacion)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("getdate()")
                    .ValueGeneratedOnAdd()
                    .IsRequired();
    
    
                modelBuilder.Entity<Loteria>()
                    .Property(p => p.FechaModificacion)
                    .HasColumnType("datetime")                
                    .HasDefaultValueSql("getdate()")
                    .ValueGeneratedOnAddOrUpdate()
                    .IsRequired();
    
    

    ​I have also tried like this

     modelBuilder.Entity<Loteria>()
                    .Property(p => p.FechaCreacion)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("getdate()")
                    .IsRequired();
    
    
                modelBuilder.Entity<Loteria>()
                    .Property(p => p.FechaModificacion)
                    .HasColumnType("datetime")                
                    .HasDefaultValueSql("getdate()")
                    .IsRequired();
    

    But all I get is:

    > An unhandled exception occurred while processing the request.
    > 
    > SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.  
    > System.Data.SqlClient.SqlCommand+  
    > <>c.<ExecuteDbDataReaderAsync>b__108_0(Task<SqlDataReader> result)
    >
    > DbUpdateException: An error occurred while updating the entries. See the inner exception for details.  
    > Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+<ExecuteAsync>d__32.MoveNext()
    
    Wednesday, February 21, 2018 3:02 PM

All replies

  • Hi Luis Tejada,

    Thank you for posting here.

    For your question is more related to EF, I will move it to ADO.NET Entity Framework and LINQ to Entities forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 22, 2018 7:10 AM