0

j'ai donc une relation simple entre une entité et un type complexe, et je veux en informer l'entité lorsque les changements de type complexe, comme dans ce codeComment élever événement PropertyChanged sur les propriétés chargées paresseux

[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; 
} 

Répondre

0

Oh, je viens de réaliser que virtual signifie qu'il peut avoir un corps, et heureusement EntityFramework appels que le corps après la fin de chargement paresseux, donc cela a parfaitement fonctionné

[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; 
}