我有一个具有值对象的实体,并且该值对象具有另一个值对象。我的问题是,当更新实体和值对象时,具有父值对象的实体得到更新,但子值对象没有。注意,我使用了最新版本的 Entity Framework Core 2.1.0-rc1-final。
这是父实体Employee
:
public class Employee : Entity
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Email { get; private set; }
public Address Address { get; private set; }
}
这是父值对象Address
:
public class Address : ValueObject<Address>
{
private Address() { }
public Address(string street, string city, string state, string country, string zipcode, GeoLocation geoLocation)
{
Street = street;
City = city;
State = state;
Country = country;
ZipCode = zipcode;
GeoLocation = geoLocation;
}
public string Street { get; private set; }
public string City { get; private set; }
public string State { get; private set; }
public string Country { get; private set; }
public string ZipCode { get; private set; }
public GeoLocation GeoLocation { get; private set; }
}
这是子值对象GeoLocation
:
public class GeoLocation
{
private GeoLocation()
{
}
public GeoLocation(decimal longitude, decimal latitude)
{
Latitude = latitude;
Longitude = longitude;
}
public Decimal Longitude { get; private set; }
public Decimal Latitude { get; private set; }
}
在更新员工时,我首先从数据库中获取它,然后Address
使用从用户界面获得的新值更改属性。
var employee = _repository.GetEmployee(empId);
employee.SetAddress(newAddress);
和SetAddress
方法:
public void SetAddress(Address address)
{
Guard.AssertArgumentNotNull(address, nameof(address));
Address = address;
}