question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can ValueGenerator be applied in add and update situation?

See original GitHub issue

Steps to reproduce

Summary: I am trying to understand the ValueGenerator | .HasValueGenerator feature. To do so I am trying to create a simple tracking example where I saved the username of the person that created, and the username of person that updated a class.

The issue

It looks like the ValueGenerator is only called on an add. Is that correct?

I can of course override SaveChanges\Async and get at the ChangeTracker to do this (which is what I do in EF6), but I wondered if there was a better way in EF Core.

For your laughter here is my code:

internal class SimpleTrackingConfig<T> where T : class, ISimpleTracking
{
    private readonly UserNameValueGenerator _valueGenerator;
    public SimpleTrackingConfig(Func<string> getCurrentUser)
    {
        _valueGenerator = new UserNameValueGenerator(getCurrentUser);
    }

    public void Configure(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<T>()
            .Property(x => x.CreatedOn)
            .HasDefaultValueSql("getutcdate()");

        modelBuilder.Entity<T>()
            .Property(x => x.CreatedBy)
            .HasMaxLength(256)
            .HasValueGenerator((p, e) => UserNameFactory());

        modelBuilder.Entity<T>()
            .Property(x => x.UpdatedOn)
            .HasComputedColumnSql("getutcdate()");

        modelBuilder.Entity<T>()
            .Property(x => x.UpdatedBy)
            .HasMaxLength(256)
            .HasValueGenerator((p, e) => UserNameFactory());
    }

    private ValueGenerator<string> UserNameFactory()
    {
        return _valueGenerator;
    }

    private class UserNameValueGenerator : ValueGenerator<string>
    {
        private readonly Func<string> _getCurrentUser;

        public UserNameValueGenerator(Func<string> getCurrentUser)
        {
            _getCurrentUser = getCurrentUser;
        }

        public override bool GeneratesTemporaryValues => false;

        public override string Next(EntityEntry entry)
        {
            return _getCurrentUser == null ? "" : _getCurrentUser();
        }
    }
}

Further technical details

EF Core version: “Microsoft.EntityFrameworkCore.SqlServer”: “1.1.0-preview1-final” Operating system: Windows 10 Visual Studio version: VS2015 update 3

Other details about my project setup:

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:35
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Dzivocommented, Sep 23, 2021

2021 any news on this ?

3reactions
pokecommented, May 2, 2019

I would like to ask for this functionality for updates as well.

I have an entity that has a Name and a NormalizedName which is normalized based on the Name using a fixed rule. This makes it possible to have NormalizedName as an index and look up entities based on it. It works equialent to ASP.NET Core Identity’s NormalizedUserName and NormalizedEmail.

Now, I would like to use a value generator to make sure that this normalized column is always calculated properly on top off the current Name value. Identity solves this by basically updating it on every change through the UserManager, but I would like to move this to the database if possible.

Using a ValueGenerator already allows me to do this on adds; but I would like to have this run on updates too, so I don’t have to go through higher-level services just to enforce this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can ValueGenerator be applied in add and update situation?
It looks like the ValueGenerator is only called on an add. Is that correct? I can of course override SaveChanges\Async and get at...
Read more >
Generated Values - EF Core
However, specifying ValueGeneratedOnAdd on a DateTime property will have no effect (see the section below for DateTime value generation).
Read more >
Hibernate : @ValueGenerationType applied at insert but ...
It seems that you can try to annotate your property date as below. @Temporal(TemporalType.TIMESTAMP) @Generated(GenerationTime.
Read more >
Value Generator
The add-on allows the user to modify the values and names of existing fields. This can be done by selecting the field that...
Read more >
EF Core 7: It Just Keeps Getting Better
It's faster, it allows bulk updates and deletes, it lets you map entity properties to. ... but those do add up in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found