0

,所以我有一个实体和复杂类型之间的简单关系PropertyChanged事件,我想通知实体时,复杂类型的变化,在此代码如何提高对延迟加载特性

[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

哦,我只是意识到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; 
}