MVVM中的存储库模式 - WPF [英] Repository pattern in MVVM - WPF

查看:151
本文介绍了MVVM中的存储库模式 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中使用Entity Framework 6。我已经创建了一个Repository,它将实体列表返回给Viewmodel。因为我需要将这些列表绑定到DataGrid,所以我将它们转换为ObservableCollection。需要这样做,以便EF6的更改跟踪器可以检测所做的更改,当我在UnitOfWork上调用Save()时,更改将保存到数据库中。怎么办呢?



我尝试过:



我在ViewModel中填充我的observablecollection,如下所示:

I am using Entity Framework 6 in WPF. I have already created a Repository which returns list of entities to the Viewmodel. Since I need to bind these lists to the DataGrid, I have converted these to ObservableCollection. It needs to be done so that the change tracker of EF6 can detect the changes made, and when I call Save() on UnitOfWork, the changes are saved to the database. How can that be done?

What I have tried:

I am populating my observablecollection in ViewModel like this:

ObservableCollection<items> TestCollection = new ObservableCollection<items>(UnitOfWork.Items.GetAll());





现在,如果我向TestCollection添加任何项目然后调用



Now, if I add any item to TestCollection and then call

UnitofWork.Complete()

常识指示该项目不会添加到数据库中。当然,它不是。

common sense dictates that the item would not be added to the database. And of course, it isn't.

推荐答案

谷歌搜索: wpf c#mvvm实体框架绑定 - Google搜索 [ ^ ]有一些可以回答有关DataGrid EF绑定的问题的链接:



* 基本WPF教程 - 使用实体框架的MVVM项目 - 代码项目 [ ^ ]



* Windows实体框架MVVM遍历C#中的1个示例,Visual Studio 2013的XAML [ ^ ]
Google Search: wpf c# mvvm entity framework binding - Google Search[^] has a number of links that can answer your question regarding the DataGrid EF binding:

* Tutorial for a Basic WPF – MVVM Project Using Entity Framework - Code Project[^]

* Windows Entity Framework MVVM Walk Through 1 sample in C#, XAML for Visual Studio 2013[^]


这篇关于MVVM中的存储库模式 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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