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

Why ConcurrencyStamp not change when ValueObject updated #19962

Closed
2501272126 opened this issue May 31, 2024 · 1 comment · Fixed by #20012
Closed

Why ConcurrencyStamp not change when ValueObject updated #19962

2501272126 opened this issue May 31, 2024 · 1 comment · Fixed by #20012
Assignees
Milestone

Comments

@2501272126
Copy link

2501272126 commented May 31, 2024

code example:

public class DateRange : ValueObject {
    public DateRange(DateOnly start, DateOnly end) {
        if (start > end) {
            throw new InvalidDataException("");
        }
        Start = start;
        End = end;
    }

    public DateOnly Start { get; private init; }

    public DateOnly End { get; private init; }

    protected override IEnumerable<object> GetAtomicValues() {
        yield return Start;
        yield return End;
    }
}

public class Program : AggregateRoot<Guid> {
    public string Name { get; private set; }
    public DateRange DateRange { get; private set; }
    public List<Item> Items { get; private init; } = new();

    public void Update(string name, DateRange dateRange, IEnumerable<Item> items) {
          Name = name;
          DateRange = dateRange;
          Items.Clear();
          Items.AddRange(items);
      }
}

var program = await _programRepository.GetAsync(id);
program.Update(name, dateRange, items);
await _programRepository.UpdateAsync(program, true);

when i update the Name field ConcurrencyStamp is worked,
but when i update the DateRange or Items , database has changed and the ConcurrencyStamp not update.

@maliming maliming self-assigned this Jun 3, 2024
@maliming maliming added this to the 8.3-preview milestone Jun 3, 2024
@maliming
Copy link
Member

hi

Can you try to override the HandlePropertiesBeforeSave method of AbpDbContext.

protected virtual void HandlePropertiesBeforeSave()
{
    var entries = ChangeTracker.Entries().ToList();
    foreach (var entry in entries)
    {
        HandleExtraPropertiesOnSave(entry);

        if (entry.State.IsIn(EntityState.Modified, EntityState.Deleted))
        {
            UpdateConcurrencyStamp(entry);
        }
    }

    if (EntityChangeOptions.Value.PublishEntityUpdatedEventWhenNavigationChanges)
    {
        foreach (var entry in AbpEfCoreNavigationHelper.GetChangedEntityEntries().Where(x => x.State == EntityState.Unchanged))
        {
            UpdateConcurrencyStamp(entry);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants