C# 更改模型中的不同属性时更新实体框架属性

C# 更改模型中的不同属性时更新实体框架属性,c#,entity-framework,asp.net-mvc-4,observablecollection,inotifypropertychanged,C#,Entity Framework,Asp.net Mvc 4,Observablecollection,Inotifypropertychanged,我有一个实体框架模型 public partial class Product { public Product() { this.Designs = new HashSet<Design>(); } public int BookingProductId { get; set; } public System.Guid BookingId { get; set; } public decimal Price { ge

我有一个实体框架模型

public partial class Product
{
    public Product()
    {
        this.Designs = new HashSet<Design>();
    }

    public int BookingProductId { get; set; }
    public System.Guid BookingId { get; set; }
    public decimal Price { get; set; }

    public virtual Booking Booking { get; set; }
    public virtual ICollection<Design> Designs { get; set; }
}
然后NotifyPropertyChanged方法永远不会被激发。有人知道为什么吗


提前感谢

NotifyPropertyChanged从不触发,因为向设计集合添加另一个元素实际上不会更改任何产品的属性。如果您想跟踪集合本身内的更改,它应该实现接口。ObservableCollection已经实现了这一点,因此您可以简单地处理CollectionChanged事件。

NotfiyPropertyChanged将仅在您设置整个集合而不是包含的项时调用

这是一个解决问题的方法:

public partial class Product : INotifyPropertyChanged
{
    public Product()
    {
        this.Designs = new ObservableCollection<Design>();
        this.Designs.CollectionChanged += ContentCollectionChanged;
    }

    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // This will get called when the collection is changed
        // HERE YOU CAN ALSO FIRE THE PROPERTY CHANGED 
    }

    public int BookingProductId { get; set; }
    public System.Guid BookingId { get; set; }
    public decimal Price { get; set; }

    public virtual Booking Booking { get; set; }
    public virtual ObservableCollection<Design> Designs { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
公共部分类产品:INotifyPropertyChanged
{
公共产品()
{
this.Designs=新的ObservableCollection();
this.Designs.CollectionChanged+=ContentCollectionChanged;
}
public void ContentCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
//当集合发生更改时,将调用此函数
//在这里,您还可以对更改的属性进行激发
}
public int BookingProductId{get;set;}
public System.Guid BookingId{get;set;}
公共十进制价格{get;set;}
公共虚拟预订{get;set;}
公共虚拟可观察集合设计{get;set;}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

基于对其他问题的一些回答,我想知道您是否应该看看
INotifyCollectionChanged
事件。太好了,非常感谢。我现在还注意到,当我从数据库中检索产品对象时,会触发ContentCollectionChanged事件,并且.Include设计,这很有意义,但我想知道是否有任何方法可以将其与我在.Add(new Design());???时的行为区分开来???通常您不必这样做,假设您正在使用MVVM模式,并且已经将集合绑定到一个列表框。列表框将触发此事件,例如SelectedItem(s)Changed(已更改)等。不确定我是否理解…我可以重新措辞吗?我已从DB:var Product=this.GetDbSet().Include(c=>c.BookingDesigns).Single(c=>c.bookingdroductid==bookingdroductid)检索到一个产品和相关设计;此时,每次向产品添加设计时,都会触发ContentCollectionChanged。然后我想添加一个新的设计…product.Designs.add(new Design());ContentCollectionChanged也会被激发。在这一点上,我希望对产品的价格属性执行操作,而不是在从DB检索时执行操作。这有意义吗?
product.Designs.Add(new Design());
public partial class Product : INotifyPropertyChanged
{
    public Product()
    {
        this.Designs = new ObservableCollection<Design>();
        this.Designs.CollectionChanged += ContentCollectionChanged;
    }

    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // This will get called when the collection is changed
        // HERE YOU CAN ALSO FIRE THE PROPERTY CHANGED 
    }

    public int BookingProductId { get; set; }
    public System.Guid BookingId { get; set; }
    public decimal Price { get; set; }

    public virtual Booking Booking { get; set; }
    public virtual ObservableCollection<Design> Designs { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}