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

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

问题描述

我有一个 SQL 数据库并且正在实现一个 WPF UI 来更新它.如果我使用 EF5 从数据库生成类,我如何在生成的类和属性上实现 INotifyPropertyChanged 以便我可以轻松地使用 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?

谢谢

推荐答案

如果您遵循推荐的 MVVM 模式,您可以将生成的类视为模型,然后编写实现 INotifyPropertyChanged 的​​ ViewModel 包装器.ViewModel 类将访问您的数据库类并公开您可以在 XAML 中绑定到您的 UI 的属性.

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.

虽然一开始需要做更多的工作,但从长远来看,如果您需要进行任何中间格式化或处理,或者如果您需要在不影响 UI 的情况下更改数据库类,MVVM 模式绝对可以带来回报.

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天全站免登陆