在生成的实体框架类上实现INotifyPropertyChanged [英] Implement INotifyPropertyChanged on generated Entity Framework classes

查看:124
本文介绍了在生成的实体框架类上实现INotifyPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL数据库,并正在实现一个WPF UI来更新它。如果我使用EF5从DB生成类,那么如何在生成的类和属性上实现INotifyPropertyChanged,以便我可以使用UI轻松地绑定到它们?有一个简单的方法来实现吗?



谢谢

解决方案

如果您按照WPF推荐的 MVVM 模式,您可以对待生成的类作为Model,然后编写实现INotifyPropertyChanged的ViewModel包装器。 ViewModel类将访问您的数据库类,并公开您可以在XAML中绑定到UI的属性。



如您的评论所述,这可能会导致很多工作编写样板代码,但有一些方法可以处理。请参阅此问题一些想法



起初更多的工作,如果您需要进行任何中间格式化或处理,或者如果需要更改,MVVM模式可以从长远来看能够得到回报您的数据库类,而不影响UI。


I have an SQL DB and am implementing a WPF UI to update it. If I use EF5 to generate the classes from the DB, how can I implement INotifyPropertyChanged on the generated classes and properties so I can easily bind to them with the UI? Is there an easy way to achieve this?

Thanks

解决方案

If you follow the recommended MVVM pattern for WPF, you can treat your generated classes as the Model, and then write ViewModel wrappers that implement INotifyPropertyChanged. The ViewModel classes would access your DB classes and expose properties that you can bind to your UI in XAML.

As noted in your comment, this can result in a lot of work writing boilerplate code, but there are some ways to deal with that. See this question for some ideas.

While more work at first, the MVVM pattern can definitely pay off in the long run if you ever need to do any intermediate formatting or processing, or if you need to change your database classes without affecting the UI.

这篇关于在生成的实体框架类上实现INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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