我们遇到了同样的问题,我们找到了解决该问题的方法。只是我们包裹双?属性在我们的基础实体中翻倍;Hier 是代码:
Notmapped 实体(Wrapper 实体)应该从外部使用它,另一个将从 DB 存储和加载。
继承的实体代码:
[NotMapped]
public double Longitude
{
get
{
return this.FromNullable(this.longitude);
}
set
{
this.PropertyChange(ref this.longitude, value);
}
}
[Column("Longitude")]
public double? LongitudeStored
{
get
{
return this.longitude;
}
set
{
this.PropertyChange(ref this.longitude, value);
}
}
Hier 是基本实体代码:
protected double FromNullable(double? value)
{
return value.HasValue ? value.Value : double.NaN;
}
protected void PropertyChange(ref double? propertyValue, double newValue, [CallerMemberName] string propertyName = "")
{
this.PropertyChangeCore(ref propertyValue, double.IsNaN(newValue) ? (double?)null : (double?)newValue, propertyName);
}
protected void HandlePropertyCore(ref double? propertyValue, double? newValue, [CallerMemberName] string propertyName = "")
{
if ((newValue.HasValue || propertyValue.HasValue) && // If both are null than do not fire.
((!propertyValue.HasValue || double.IsNaN(propertyValue.Value))
^ (!newValue.HasValue || double.IsNaN(newValue.Value)) // If one of them is null or NaN then fire according to XOr rule.
|| Math.Abs(propertyValue.Value - newValue.Value) > double.Epsilon)) // If the are not the same than fire.
{
propertyValue = newValue;
this.HandlePropertyCore(propertyName); // HERE YOU NEED JUST TO HANDLE DOUBLE PROPERTY
}
}