You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When configuring a property as ValueGeneratedOnAddOrUpdate and setting its AfterSaveBehavior to Save, the expectation is to be able to provide an explicit value. This works when updating, but not when adding.
Repro
awaitusingvarctx=newBlogContext();awaitctx.Database.EnsureDeletedAsync();awaitctx.Database.EnsureCreatedAsync();ctx.Blogs.Add(newBlog{Url="foo",LastUpdated=new(2020,1,1)});awaitctx.SaveChangesAsync();ctx.ChangeTracker.Clear();varblog=ctx.Blogs.Single();Console.WriteLine("LastUpdated after adding: "+blog.LastUpdated);// Null, value was ignoredblog.LastUpdated=new(1990,1,1);awaitctx.SaveChangesAsync();ctx.ChangeTracker.Clear();blog=ctx.Blogs.Single();Console.WriteLine("LastUpdated after updating: "+blog.LastUpdated);// 1990-01-01, value was savedpublicclassBlogContext:DbContext{publicDbSet<Blog>Blogs{get;set;}staticILoggerFactoryContextLoggerFactory=>LoggerFactory.Create(b =>b.AddConsole().AddFilter("",LogLevel.Information));protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer(@"...").EnableSensitiveDataLogging().UseLoggerFactory(ContextLoggerFactory);protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Blog>().Property(b =>b.LastUpdated).ValueGeneratedOnAddOrUpdate().Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save);}}publicclassBlog{publicintBlogId{get;set;}publicstringUrl{get;set;}publicDateTime?LastUpdated{get;set;}}
The text was updated successfully, but these errors were encountered:
This is because SQL Server at least does not allow computed column values to be inserted, and we didn't want the default experience on SQL Server with computed properties to throw.
If I set the before-save behavior: property.SetBeforeSaveBehavior(PropertySaveBehavior.Save);, then I see the value being inserted as expected.
When configuring a property as ValueGeneratedOnAddOrUpdate and setting its AfterSaveBehavior to Save, the expectation is to be able to provide an explicit value. This works when updating, but not when adding.
Repro
The text was updated successfully, but these errors were encountered: