Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add explicit value when adding for a property with AfterSaveBehavior=Save #23912

Closed
roji opened this issue Jan 18, 2021 · 1 comment
Closed

Comments

@roji
Copy link
Member

roji commented Jan 18, 2021

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
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

ctx.Blogs.Add(new Blog { Url = "foo", LastUpdated = new(2020, 1, 1) });
await ctx.SaveChangesAsync();

ctx.ChangeTracker.Clear();
var blog = ctx.Blogs.Single();
Console.WriteLine("LastUpdated after adding: " + blog.LastUpdated); // Null, value was ignored

blog.LastUpdated = new(1990, 1, 1);
await ctx.SaveChangesAsync();

ctx.ChangeTracker.Clear();
blog = ctx.Blogs.Single();
Console.WriteLine("LastUpdated after updating: " + blog.LastUpdated); // 1990-01-01, value was saved

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"...")
            .EnableSensitiveDataLogging()
            .UseLoggerFactory(ContextLoggerFactory);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>().Property(b => b.LastUpdated)
            .ValueGeneratedOnAddOrUpdate()
            .Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save);
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public DateTime? LastUpdated { get; set; }
}
@ajcvickers
Copy link
Contributor

The default before-save behavior for ValueGeneratedOnAddOrUpdate properties is Ignore. The model in this repro does not change this:

Model: 
  EntityType: Blog
    Properties: 
      BlogId (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
      LastUpdated (Nullable<DateTime>) BeforeSave:Ignore ValueGenerated.OnAddOrUpdate
      Url (string)
    Keys: 
      BlogId PK

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.

@ajcvickers ajcvickers removed this from the 6.0.0 milestone Mar 27, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants