EF EntityObject不更新关系的数据绑定 [英] EF EntityObject not updating databindings of relationships

查看:230
本文介绍了EF EntityObject不更新关系的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用了EntityFramework,WPF和MVVM,并且在更新EntityObjects之间关系的数据绑定方面遇到了一些问题。我能够将我的问题缩小到只有几行XAML,我希望有人可以帮助我,因为我对EF和MVVM仍然不够自信。



无论如何,这里我们用简化的XAML:

 < DatePicker Grid.Row =2Grid.Column =1 
SelectedDate ={Binding Path = File.SentDate,
StringFormat = {} {0:dd / MM / yyyy},Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}
VerticalAlignment =Center IsEnabled ={Binding Path = IsEnabled}/>

< ComboBox Grid.Row =3Grid.Column =1ItemsSource ={Binding Contacts}DisplayMemberPath =Name
SelectedItem ={Binding Path = File .Sender,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}IsEditable =True
VerticalAlignment =Center>
< / ComboBox>

< Label Content ={Binding Path = File.SenderId}Grid.Row =4/>
< Label Content ={Binding Path = File.Sender.Name}Grid.Row =5/>
< Label Content ={Binding Path = File.SentDate}Grid.Row =6/>

我使用最后3个标签来测试我的数据绑定。使用DatePicker更改File.SentDate将数据绑定更新为最后一个标签,无任何问题。



现在文件的类型为EntityObject,并且具有类型为GUID的SenderId属性。它也通过Sender属性与我的联系人有关系。有意思的是,SenderId是通过发件人关系与文件相关的相应Contact EntityObject的GUID。一个文件只能有一个联系人的单个发件人。



无论如何,当我使用组合框选择另一个发件人时,显示File.SenderId属性的Label得到正确更新。但是,具有File.Sender.Name属性(即使用关系的属性)的那个属性不会被更新。



所以我猜想有更新EF中关系数据绑定的一些特别的东西。



可以有人请你提出一个解决方案?

解决方案

不幸的是,实体框架不会在关联属性更改时通知。这就是您的Binding不起作用的原因。



该问题向Microsoft报告: http://connect.microsoft.com/VisualStudio/feedback/details/532257/ entity-framework-navigation-properties-don-t-raise-the-propertychanged-event



另一种解决方法由 BookLibrary 示例应用 WPF应用程序框架(WAF) 。 Book类收听AssociationChanged事件并引发相应的PropertyChanged事件。

  public Book()
{
...
LendToReference.AssociationChanged + = LendToReferenceAssociationChanged;
}

private void LendToReferenceAssociationChanged(object sender,
CollectionChangeEventArgs e)
{
//导航属性LendTo不支持PropertyChanged事件。
//我们必须自己提高。
OnPropertyChanged(LendTo);
}


I'm using EntityFramework, WPF and MVVM in my application and got some problems with updating the databinding of relationships between EntityObjects. I was able to downsize my problem to only a few lines of XAML and I hope someone can help me as I'm still not very confident with EF and MVVM.

Anyway, here we go with the simplified XAML:

    <DatePicker Grid.Row="2" Grid.Column="1" 
                    SelectedDate="{Binding Path=File.SentDate, 
StringFormat={}{0:dd/MM/yyyy}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                    VerticalAlignment="Center" IsEnabled="{Binding Path=IsEnabled}"/>

        <ComboBox Grid.Row="3" Grid.Column="1" ItemsSource="{Binding Contacts}" DisplayMemberPath="Name" 
                  SelectedItem="{Binding Path=File.Sender, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEditable="True"
                  VerticalAlignment="Center">
        </ComboBox>

        <Label Content="{Binding Path=File.SenderId}" Grid.Row="4"/>
        <Label Content="{Binding Path=File.Sender.Name}" Grid.Row="5"/>
        <Label Content="{Binding Path=File.SentDate}" Grid.Row="6"/>

I'm using the last 3 Labels to test my databinding. Changing the File.SentDate using the DatePicker updates the databinding to the last Label without problem.

Now File is of type EntityObject and has a SenderId property of type GUID. It also has a relationship to my Contacts through the Sender property. Obvisouly, SenderId is the GUID of the corresponding Contact EntityObject which is related to File through the Sender relationship. A File can have only 1 single Sender of type Contact.

Anyway, what happens is that when I select another sender using the combobox, the Label displaying the File.SenderId property get properly updated. However, the one with the File.Sender.Name property i.e. the one using the reléationship does not get updated.

So I'm guessing that there is something special about updating the databinding of relationships in EF.

Can someone please suggest a solution to this?

解决方案

Unfortunately, the Entity Framework doesn’t notify when an association property changes. That’s the reason why your Binding didn’t work.

The issue is reported to Microsoft: http://connect.microsoft.com/VisualStudio/feedback/details/532257/entity-framework-navigation-properties-don-t-raise-the-propertychanged-event

Another workaround is shown by the BookLibrary sample application of the WPF Application Framework (WAF). The Book class listens to the AssociationChanged event and raises the appropriate PropertyChanged event.

public Book()
{
    …
    LendToReference.AssociationChanged += LendToReferenceAssociationChanged;
}

private void LendToReferenceAssociationChanged(object sender, 
        CollectionChangeEventArgs e)
{
    // The navigation property LendTo doesn't support the PropertyChanged event. 
    // We have to raise it ourselves.
    OnPropertyChanged("LendTo");
}

这篇关于EF EntityObject不更新关系的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆