locked
defaulted dates overridden every time I save changes RRS feed

  • Question

  • User781011338 posted

    I have used:


    modelBuilder.Entity<PersonSurveyed>()
    .Property(p => p.Created)
    .HasDefaultValueSql("getdate()")
    .ValueGeneratedOnAdd();

    modelBuilder.Entity<PersonSurveyed>()
    .Property(p => p.LastUpdated)
    .HasDefaultValueSql("getdate()")
    .ValueGeneratedOnAdd();

    in OnModelCreating to generate defaults for properties when they are created. This works fine on create but when I save the records values created with these defaults get set to null. This is not happneing database side because when I go and do these transaction manually the values are left alone. 


    public class EntityBase
    {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Display(Name = "Created")]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy h:m:s tt}")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public DateTime Created { get; }
    [Display(Name = "Last Updated")]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy h:m:s tt}")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime LastUpdated { get; }
    }

    As you can see there is no set so the properties shouldn't be being written to when I save changes.

    Any help would be very welcome.

    Thanks

    Friday, April 10, 2020 8:23 AM

All replies

  • User475983607 posted

    adenjones@gmail.com

    As you can see there is no set so the properties shouldn't be being written to when I save changes.

    According to the documentation the code is functioning as expected.   HasDefaultValueSql and [DatabaseGenerated(DatabaseGeneratedOption.Identity)] affect entities that are added and not set to a CLR default value.  They do not cover 

    https://docs.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations

    If you add an entity to the context that has a value assigned to the property, then EF will attempt to insert that value rather than generating a new one. A property is considered to have a value assigned if it is not assigned the CLR default value (null for string0 for intGuid.Empty for Guid, etc.). For more information, see Explicit values for generated properties.

    I recommend reading the linked documentation as it explains how to handle Add and Updated entity values. 

    If you still need community support than share enough code to reproduce this issue.  Also, please format your source code on the forum using the Insert/Edit code sample button {;}.

    Friday, April 10, 2020 11:52 AM
  • User-854763662 posted

    Hi adenjones@gmail.com ,

    This works fine on create but when I save the records values created with these defaults get set to null.

    What do you mean by "this works fine on create" and "set to null" ?

    I failed to reproduce the issue. Could you share the specific action code and the screenshots of related result? 

    Best Regards,

    Sherry

    Monday, April 13, 2020 9:41 AM
  • User781011338 posted

    on further investigation the problem seems to be with SaveChanges. I got around the problem by retrieving the object and only inserting the values the have been changed by a view and then saving this object. By default the generated views passed the entire object back to the database and overrode the value even though the value was not supposed to be set. I see what was happening now but can't think of another way around it. Thanks for your input.

    Tuesday, April 14, 2020 11:36 AM
  • User781011338 posted

    Thanks for your feedback. I have resolved the issue. I will try to remember to format code so it is more readable in future. Cheers

    Tuesday, April 14, 2020 11:37 AM