简体   繁体   English

与ViewModels共享EntityFramework上下文

[英]Sharing EntityFramework Context with ViewModels

I use EF 6.1 alpha with Caliburn.Micro 2.0 -alpha2. 我将Calibre.Micro 2.0 -alpha2与EF 6.1 alpha一起使用。

I have a ViewModel called ProductWorkspaceViewModel, which contains a ProductViewModel. 我有一个名为ProductWorkspaceViewModel的ViewModel,其中包含一个ProductViewModel。 Both of these ViewModels using EF with respective Context, while the first read all Products from Db and shows them in a list and the later do CRUD operation on the selected item. 这两个ViewModel都使用带有各自上下文的EF,而第一个ViewModels从db中读取所有Product并将其显示在列表中,随后对所选项目执行CRUD操作。

Now, when I delete, add, or update the Product from ProductViewModel, it should get updated in the ProductWorkSpaceViewModel. 现在,当我从ProductViewModel删除,添加或更新产品时,它应该在ProductWorkSpaceViewModel中得到更新。 I update the Workspace with EventAggregator and either use LoadAll() or Send the update ProductEntity to the WorkSpaceViewModel. 我使用EventAggregator更新工作区,并使用LoadAll()或将更新的ProductEntity发送到WorkSpaceViewModel。 But when I select the same updated Product from Workspace, the Related Properties which are Lazy are not updated in ProductViewModel, because both have a different instance of Context. 但是,当我从工作区中选择相同的更新产品时,由于Lazy的相关属性都具有不同的Context实例,因此不会在ProductViewModel中对其进行更新。

I am think of using a shared Context for both of these ViewModel, but have read on other questions, like Context should not be shared and should be disposed soon after operation completed like delete. 我想为这两个ViewModel使用共享的Context,但是已经阅读了其他问题,例如不应共享Context,并且应该在操作完成后(如删除)立即处置Context。

How can I best achieve the solution for above problem. 我如何才能最好地解决上述问题。 Also the Workspace Context is used by many DataTemplates. 此外,许多数据模板都使用工作区上下文。

Should I use a shared Context or I should update either ViewModel with all Lazy Properties loaded. 我应该使用共享的上下文,还是更新所有已加载的惰性属性的ViewModel。 But that will add more code and is complex. 但这将增加更多代码,并且很复杂。

Looking for a Best Practise. 寻找最佳实践。

Upate: I just mentioned DbContext for reference, I use a DAL which has a class that Inherits from DbContext. Upate:我刚刚提到了DbContext供参考,我使用的DAL具有从DbContext继承的类。

Also I want to update that my Main project doesn't use either EF or any DAL. 另外,我想更新主项目不使用EF或任何DAL。 It acts as a host to all modules that I have created as Sub Project. 它充当了我作为子项目创建的所有模块的宿主。 For eg., ProductViewModel is part of Inventory Module, which I load into Main.exe using IoC (MEF) 例如,ProductViewModel是库存模块的一部分,我使用IoC(MEF)将其加载到Main.exe中

The best practice is for your ViewModel s not to use the Entity Framework or it's DataContext s directly but to have a Data Access Layer which takes care of all the DB operations instead and Your VMs should use a Service Provider to communicate with your DAL. 最佳实践是,您的ViewModel不要直接使用Entity Framework或DataContext ,而要拥有一个负责所有DB操作的数据访问层,并且您的VM应该使用服务提供商与DAL进行通信。

As a matter of fact you shouldn't even have a reference to Entity Framework in your main project but in your DAL Class Library only 实际上,您甚至不应该在主项目中引用实体框架,而应仅在DAL类库中引用

In your VMs you should use an ObservableCollection of Product classes and both your VMs should work with that same ObservableCollection in a Parent-Child mode in which case any changes you make to a Product in your ProductViewModel will immediately affect your ProductWorkspaceViewModel . 在您的VM中,您应该使用Product类的ObservableCollection ,并且两个VM都应在父子模式下与该ObservableCollection一起工作,在这种情况下,您对ProductViewModelProduct进行的任何更改都会立即影响ProductWorkspaceViewModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM