HasDefaultValueSql("getutcdate()").ValueGeneratedOnAdd(); not working as expected

David Thielen 2,276 Reputation points
2023-03-19T18:40:51.34+00:00

I configure my model as follows:

public class CampaignConfiguration : IEntityTypeConfiguration<Campaign>
{
    /// <inheritdoc />
    public void Configure(EntityTypeBuilder<Campaign> builder)
    {
        builder.HasIndex(new[] { "Name", "StateId" }).IsUnique();
        builder.Property(b => b.Created).HasDefaultValueSql("getutcdate()").ValueGeneratedOnAdd();
    }
}

I then run the following code (xUnit test):

   var daveForPres = new Campaign
        {
            Name = "  Dave for President  ",
            State = colorado
        };
        // the constructor set the Created to now - honk that up before the save.
        daveForPres.SetPrivatePropertyValue("Created", new DateTime(1955, 09, 26));

        context.Campaigns.Add(daveForPres);
        context.SaveChanges();

And when I look in the database (via SSRS), the Created date is set to 1955-09-26.

Why is it using the value in my model object? It's supposed to be ignoring that.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2023-03-22T12:47:33.44+00:00

    @David Thielen, Welcome to Microsoft Q&A, based on my test, I reproduced your problem. If you want to Created is always the value of getutcdate(). We need to do some extra operations for the current code.

    You could try the following code:

     modelBuilder.Property(b => b.Created).HasDefaultValueSql("getutcdate()").ValueGeneratedOnAdd().Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore);
    

    Based on my test, it will ignore the new inserted value and still use the value of getutcdate().

    Like the following code:

    var daveForPres = new Campaign
                {
                    Name = "  Dave for President  ",
                     StateId=1001
                     
                };
                daveForPres.Created = DateTime.Parse("1996-02-20");
                Mycontext mycontext = new Mycontext();
                mycontext.Campaigns.Add(daveForPres);
                mycontext.SaveChanges();
    
    

    I still get the current date in database:

    User's image

    Hope my solution could be helpful.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Paolo Lazzaroni 0 Reputation points
    2023-03-19T20:49:04.1066667+00:00

    It seems that the HasDefaultValueSql("getutcdate()").ValueGeneratedOnAdd(); is not working as expected. The Created property is set to the value of getutcdate() when a new Campaign object is created. However, when you set the Created property to a specific date, it is not being ignored and is being saved to the database.

    It is possible that the ValueGeneratedOnAdd() is not being applied correctly. You can try removing the ValueGeneratedOnAdd() method and see if it works as expected.

    You can also try setting the Created property to null before saving the Campaign object. This should force the database to use the default value of getutcdate().

    I hope this helps! Let me know if you have any other questions.

    0 comments No comments