@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:
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.