所以我在实体和复杂类型之间有一个简单的关系,我想在复杂类型发生变化时通知实体,如此代码
[Table("Bills")]
public class Bill : NotifyBase
{
//how to call SetWithNotif when this changes ?
public virtual Discount Discount { get; set; }
}
[ComplexType]
public class Discount : NotifyBase
{
//some props in here
}
public class NotifyBase : INotifyPropertyChanged,INotifyPropertyChanging
{
public void SetWithNotif<T>(T val,ref T field,[CallerMemberName] string prop = "" )
{
if (!field.Equals(val))
{
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(prop));
field = val;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
}
[field: NotMapped]
public event PropertyChangedEventHandler PropertyChanged;
[field: NotMapped]
public event PropertyChangingEventHandler PropertyChanging;
}
答案 0 :(得分:0)
哦,我刚刚意识到virtual
意味着它可以有一个正文,幸运的是EntityFramework
在完成延迟加载后调用该正文,所以这完美地运行了
[Table("Bills")]
public class Bill : NotifyBase
{
//works !!
private Discount m_Discount;
public virtual Discount Discount
{
get { return m_Discount; }
set { SetWithNotif(value, ref m_Discount); }
}
}
[ComplexType]
public class Discount : NotifyBase
{
//some props in here
}
public class NotifyBase : INotifyPropertyChanged,INotifyPropertyChanging
{
public void SetWithNotif<T>(T val,ref T field,[CallerMemberName] string prop = "" )
{
if (!field.Equals(val))
{
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(prop));
field = val;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
}
[field: NotMapped]
public event PropertyChangedEventHandler PropertyChanged;
[field: NotMapped]
public event PropertyChangingEventHandler PropertyChanging;
}