探索具有用于每个视图模型一个的DbContext的缺陷(如果有的话)/类使用它 [英] Exploring the pitfalls(if any) of having a DbContext for each ViewModel/Class that uses it

查看:123
本文介绍了探索具有用于每个视图模型一个的DbContext的缺陷(如果有的话)/类使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习实体框架,并在MVVM应用程序,其中每个视图模型工作原理与的DbContext数据访问使用它。免责声明:在实际应用中,我知道了视图模型不应该与数据访问层直接交互

I am learning the Entity Framework and using it in an MVVM app where each ViewModel works with the DbContext for Data Access. Disclaimer: In a real application I know the ViewModel shouldn't interact directly with the Data Access Layer.

由于每个视图模型有监控和操纵的状态查看通过保持与模型本身的关系,我开始怀疑起转多的DbContext对象的影响,如果事情像的DbContext最好是作为一个单独离开 - 而我很快找到了答案是的NO 。因此,如果共识是要为每个使用实例(如多的ViewModels,或者案件什么具备的,你)我还没有看到那里的任何人提到具有这种潜在的问题。

Given that each ViewModel is there to monitor and manipulate the state of the View by maintaining relationships with the Models themselves, I began to wonder about the implications of spinning up multiple DbContext objects, and if something like a DBContext is best left as a singleton - to which I quickly found the answer was "NO". So if the consensus is to have an instance for each use (as in the case with multiple ViewModels, or what-have-you) I still havent seen where any one mentions the potential issues with having this.

要详细说明,可以说我有两个的ViewModels并在每个我创建一个Context(的TestContext 的DbContext

To elaborate, lets say I have two ViewModels and in each I create a Context (TestContext inherits from DbContext) to maintain the Data Access activities during the lifetime of each ViewModel:

public class MainWindowViewModel : ViewModelBase
{
    private TestContext db = new TestContext();
    ... other code follows (properties methods etc...)...
}

public class TestViewModel: ViewModelBase
{
    private TestContext db = new TestContext();
    ... other code follows (properties methods etc...)...
}

是否有具有每班上下文的任何缺陷,可以使用它?

Are there any pitfalls with having a context in each class that can consume it?

一个这样以为嘲弄我的是,如果有可能有两种上下文不同步的其他,使得一个视图模型通过的上下文是更上的最新的方式具有比其他更近的数据。事情是这样的,我想知道。

One such thought that taunts me is if it's possible to have either context be out of sync with the other, such that one ViewModel has more recent data than the other by way of its context being more "up-to-date". Things like this I am interested in knowing.

感谢。

我不希望发现/涵盖所有情况因为这是唯一针对哪一个编码的情况。我只是想知道,如果有任何前期,或者说我不知道​​的是新来的主题明显的危险。

I dont hope to discover / cover every situation as that would be unique TO the situation against which one is coding. I just want to know if there are any "up-front" or obvious perils that I am unaware of being new to the subject.

推荐答案

实体框架和扩展的DbContext 支持的UnitOfWork 设计模式。这个想法是,你把你的逻辑上的交易隔开。正因为如此,你通常会想有自己的的DbContext 实例您的应用程序处理的每个部分或功能。

Entity Framework and by extension DbContext supports the UnitOfWork design pattern. The idea being that you keep your logical "transactions" separated. Because of this, you will usually want to have each portion or feature of your application deal with its own DbContext instance.

您可以考虑一下它的方式是,的DbContext 持有任何它从数据库中抽取一个本地副本并跟踪由用户到本地数据的所有更改。当你准备好,你告诉它推所需的更改回数据库一气呵成

The way you can think about it is that the DbContext holds a local copy of whatever it pulled from the database and tracks all changes made to the local data by the user. When you're ready, you tell it to push the required changes back to the database in one go.

有关你对坑瀑布和危险的问题;实体框架使用一种名为乐观并发默认。这意味着节省了本地更改回数据库时,并发根本没有被检查。无论您在本地的DbContext不得不被发送回数据库,无论另一个用户或应用程序中的另一个上下文是否改变了它。一个很好的文章解释说,如何去改变可以在这里找到:
http://msdn.microsoft.com/en-us/library/bb738618.aspx

For your question about pit-falls and perils; The Entity Framework uses what's called optimistic concurrency by default. This means that when saving the local changes back to the database, concurrency isn't checked at all. Whatever you had in your local DbContext is sent back to the database regardless of whether another user or another context in your application changed it. An excellent article explaining that and how to change the behaviour can be found here: http://msdn.microsoft.com/en-us/library/bb738618.aspx

这篇关于探索具有用于每个视图模型一个的DbContext的缺陷(如果有的话)/类使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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