We have an entity named Customer which has a Name Property that is a value Object. I’m sharing the entity value object and their relations and configuration below.
My question is: When I started tracking a customer entity and change the value object property is not considered as modified. I have to force it by calling
`
(_customersDbContext.Set<BoilerPlate.SampleAPI.Core.Customer>()
.Entry(customer).Member(“Name”) as ReferenceEntry).TargetEntry.State = EntityState.Modified;
`
However its not solving my problem becase I need original values of Name field when I changed Name property OriginalValues and CurrentValues setted same values. I want to get original values as getting when I select with traccking records.
Let me sampling:
Step 1 (Getting customer Entity)
var customer= await _customersDbContext.Set<BoilerPlate.SampleAPI.Core.Customer>() .FirstOrDefaultAsync(x => x.Id == 41)
Name property has firstname 1: lastname: 1 values.
Step 2 (Updating Name which is value object property)
`
customer.Update(
Gender.From(command.GenderId == null ? customer.Gender.Id : command.GenderId.Value),
Name.Of(“2”, “2”));
`
Step 3 (Cheking the original values)
var xxxx = (_customersDbContext.Set<BoilerPlate.SampleAPI.Core.Customer>().Entry(customer) .Member("Name") as ReferenceEntry).TargetEntry.OriginalValues;
Firstname and Lastname orginal values are 2 not 1.
How to avoid this problem, please help me thanks in advance
Customer.cs
public class Customer : Aggregate,
{
private Name _name;
public Name Name => _name;
}
Name.cs
public class Name : ValueObject
{
private string _firstName;
public string FirstName => _firstName;
private string _lastName;
public string LastName => _lastName;
private string _fullName;
public string FullName => _fullName;
private Name() { }
public static Name Of(
[NotNull] string firstName,
[NotNull] string lastName)
{
if (string.IsNullOrWhiteSpace(firstName) || firstName.Length is > 100 or < 2)
throw new InvalidNameException(firstName);
if (string.IsNullOrWhiteSpace(lastName) || lastName.Length is > 100 or < 2)
throw new InvalidNameException(lastName);
firstName = RemoveInlineSpaces(firstName);
lastName = RemoveInlineSpaces(lastName);
static string RemoveInlineSpaces(string text)
{
if (!string.IsNullOrEmpty(text))
return Regex.Replace(text.Trim(), @" +", " ");
return null;
}
Name customerName = new()
{
_firstName = firstName,
_lastName = lastName,
};
customerName.SetFullName();
return customerName;
}
private void SetFullName()
{
_fullName = $"{_firstName} {_lastName}";
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return _firstName;
yield return _lastName;
}
}
Entity Type Configuration .cs for Customer:
builder.OwnsOne(
x => x.Name,
a =>
{
a.Property(p => p.FirstName)
.HasColumnName(nameof(Customer.Name.FirstName).Underscore())
.IsRequired()
.HasField("_firstName")
.HasMaxLength(EfConstants.Lenght.Long);
a.Property(p => p.LastName)
.HasColumnName(nameof(Customer.Name.LastName).Underscore())
.IsRequired()
.HasField("_lastName")
.HasMaxLength(EfConstants.Lenght.Long);
a.Property(p => p.FullName)
.HasColumnName(nameof(Customer.Name.FullName).Underscore())
.IsRequired()
.HasField("_fullName")
.HasMaxLength(EfConstants.Lenght.ExtraLong);
});
Fahri Kaan Göktuna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.