Can ValueGenerator be applied in add and update situation?

Open
#6,999 11 comments 69 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp, sql
Domain
databases

Research direction

The issue centers on ValueGenerator, HasValueGenerator, ChangeTracker, and SaveChanges/SaveChangesAsync, using the supplied CreatedBy and UpdatedBy configuration. Start by examining how value generation is triggered during add and update operations, then reproduce the example and compare both paths. Done means establishing whether update-time generation is supported and documenting the appropriate behavior or limitation.

Written by the indexing model from the issue text.

Description

area-change-tracking
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:

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.