Skip to content

Regression in v10 - Forced Update of Required Property [MapToTarget] #899

@md-redwan-hossain

Description

@md-redwan-hossain

The following UpdateRoleAsync method works just fine with Mapster 7.4 but when I updated to Mapster 10 I am getting Reference constraint violatation from EF core because Mapster is modifying every other propery to default value that are not present in the DTO. I have to roll back to Mapster 7.4 to save the app from huge breaking change. How to get the old behavior back??

public record AuthorizableRoleRequest(string Name, bool IsActive);

public class AuthorizableRole : IAutoIncrementalEntity<long>
{
    public long Id { get; }
    public required string Name { get; set; }
    public required string NormalizedName { get; set; }
    public required bool IsActive { get; set; }
    public required long CreatedByUserId { get; init; }
    public required long? UpdatedByUserId { get; set; }
    public required DateTime CreatedAtUtc { get; init; }
    public required DateTime? UpdatedAtUtc { get; set; }
}

public async Task<ValueOutcome<Successful, IBadOutcome<HttpBadOutcomeTag>>> UpdateRoleAsync(
    long roleId, AuthorizableRoleRequest request, UserId userId, CancellationToken ct)
{
    var entity = await _appDbContext
        .AuthorizableRoles
        .FirstOrDefaultAsync(x => x.Id == roleId, ct);

    if (entity is null)
    {
        return new HttpBadOutcome(HttpBadOutcomeTag.NotFound);
    }

    var normalizedName = request.Name.ToUpperInvariant();

    var exists = await _appDbContext
        .AuthorizableRoles
        .AnyAsync(x => x.Id != entity.Id && x.NormalizedName == normalizedName, ct);

    if (exists)
    {
        return new HttpBadOutcome(HttpBadOutcomeTag.Conflict);
    }

    await request.BuildAdapter().AdaptToAsync(entity);
    
    entity.NormalizedName = normalizedName;
    entity.RefreshUpdateTrackingData(userId, _dateTimeProvider.UtcNow);

    _appDbContext
        .AuthorizableRoles
        .Update(entity);

    await _appDbContext.SaveChangesAsync();

    return new Successful();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions