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

Update the collection of child entities of the aggregate root always save the entity changes #18701

Closed
1 task done
JadynWong opened this issue Jan 5, 2024 · 0 comments
Closed
1 task done
Assignees
Milestone

Comments

@JadynWong
Copy link
Contributor

JadynWong commented Jan 5, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Description

Update the collection of child entities of the aggregate root always save the entity changes. AbpAuditingOptions.EntityHistorySelectors does not have any configuration, and the entity does not have the [Audited] attribute.

Reproduction Steps

Full code: JadynWong/AbpIssue@dbb60e1
Run the EfCoreBookAppServiceTests unit test.

  • Create new project
  • Add a aggregate root AppEntityWithNavigations to Domain layer and configure efcore dbcontext.
public class Book : AggregateRoot<Guid>
{
    public string Name { get; set; } = null!;

    public List<BookPicture> Pictures { get; set; } = [];

    protected Book() { }

    public Book(Guid id, string name) : base(id)
    {
        Name = name;
    }
}


public class BookPicture : Entity<Guid>
{
    public Guid BookId { get; set; }

    public string Url { get; set; }

    public BookPicture(Guid id, string url) : base(id)
    {
        Url = url;
    }
}
  • Add efcore migration and apply it.
  • Add a app service to application service layer and add interface to cotract layer.
public class BookAppService : BookStoreAppService, IBookAppService
{
    private readonly IRepository<Book, Guid> _repository;

    public BookAppService(IRepository<Book, Guid> repository)
    {
        _repository = repository;
    }

    public virtual async Task<BookDto> CreateAsync(string name)
    {
        var book = new Book(GuidGenerator.Create(), name);
        await _repository.InsertAsync(book);

        return ObjectMapper.Map<Book, BookDto>(book);
    }

    public virtual async Task AddPictureAsync(Guid id, string url)
    {
        var book = await _repository.GetAsync(id);
        book.Pictures.Add(new BookPicture(GuidGenerator.Create(), url));
        await _repository.UpdateAsync(book);
    }
}
  • Run the app and go to swagger
  • Call book create api got the id.
  • Call book add picture api
  • Go to database and check AbpEntityChanges table.

Expected behavior

AbpEntityChanges table should be empty.

Actual behavior

AbpEntityChanges table has same aggregate root update records.

Regression?

No response

Known Workarounds

No response

Version

8.0.1

User Interface

Common (Default)

Database Provider

EF Core (Default)

Tiered or separate authentication server

None (Default)

Operation System

Windows (Default)

Other information

Please check this method, it causes this problem.

protected virtual bool ShouldSaveEntityHistory(EntityEntry entityEntry, bool defaultValue = false)
{
if (entityEntry.State == EntityState.Detached)
{
return false;
}
if (entityEntry.State == EntityState.Unchanged)
{
if (entityEntry.Navigations.Any(navigationEntry => navigationEntry.IsModified))
{
return true;
}
if (entityEntry.Navigations.Where(x => x is ReferenceEntry).Cast<ReferenceEntry>().Any(x => x.TargetEntry != null && x.TargetEntry.State == EntityState.Modified))
{
return true;
}
return false;
}
var entityType = entityEntry.Metadata.ClrType;
if (!EntityHelper.IsEntity(entityType) && !EntityHelper.IsValueObject(entityType))
{
return false;
}
if (AuditingHelper.IsEntityHistoryEnabled(entityType))
{
return true;
}
return defaultValue;

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

3 participants