问题描述
我正在使用Entity Framework的DB First实现,并且遇到了需要计算列以及与视图进行交互的问题...
I'm using a DB First implementation of Entity Framework and I've run into an issue with a need for a computed column and the interaction with a view...
场景:
用户具有名字和姓氏。在列表框中,我想使用从名字和姓氏生成的显示名称。通常,足够简单。问题来自名字或姓氏被编辑时。我使用INotifyPropertyChanged扩展了实体,并在T4模板中实现了该实体。我扩展了User实体以创建DisplayName属性。在视图中,我使用基于DisplayName的CollectionViewSouorce。
User has FirstName and LastName. In a listbox I want to use a display name generated from FirstName and LastName. Generally, simple enough. The issue comes from when either the first name or last name gets edited. I've extended my entities with INotifyPropertyChanged and implemented that in the T4 template. I extended the User entity to create a DisplayName property. In the View I use a CollectionViewSouorce based on the DisplayName.
在编辑用户名时会出现问题,但不会发生通知,因为对FirstName进行了更改或姓氏,而不是DisplayName。因此,CollectionViewSource永远不会收到有关DisplayName的更改通知。
The issue comes in when a user's name is edited, but no notification occurs because the change is made to either FirstName or LastName, not DisplayName. So the CollectionViewSource is never getting a change notification for DisplayName.
我可以手动将OnPropertyChanged( DisplayName)添加到FirstName和LastName属性中的User实体,但是下次我从数据库更新模型时,该设置将被覆盖。
I could, manually, add an OnPropertyChanged("DisplayName") to the User entity in the FirstName and LastName properties, but that would be overwritten next time I update my model from the DB.
关于如何实现此功能的任何想法?
Any ideas on how to make this work?
谢谢。
J
推荐答案
对于记录...
每当更改和保存组成属性时,我都使用了部分类扩展,并以编程方式触发了DisplayName的OnPropertyChanged。
I went with a partial class extension and just programmatically triggered OnPropertyChanged for the DisplayName whenever the constituent properties were changed and saved.
这篇关于实体框架(6.x),计算列和INotify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!