Can ValueGenerator be applied in add and update situation?
See original GitHub issueSteps 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:
- Created 7 years ago
- Reactions:35
- Comments:11 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
2021 any news on this ?
I would like to ask for this functionality for updates as well.
I have an entity that has a
Name
and aNormalizedName
which is normalized based on theName
using a fixed rule. This makes it possible to haveNormalizedName
as an index and look up entities based on it. It works equialent to ASP.NET Core Identity’sNormalizedUserName
andNormalizedEmail
.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.