为什么使用实体框架时,重新启动的DbContext? [英] Why re-initiate the DbContext when using the Entity Framework?

查看:232
本文介绍了为什么使用实体框架时,重新启动的DbContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有更好的方法来使用的DbContext ,因为与WCF <工作时的,不建议设置为静态/ STRONG>。因此,我们正在创造它,我们要访问的数据库中的每个时间。

I don't know if there is a better way to use the DbContext because it is not recommended to set is as static when working with WCF. So we are creating it each time we want to access the database.

了解使用实体框架的所有优点,部分成为无用的,因为我们是在的DbContext 每次重建;更可能造成的开销,因为创建大实体模型的过程是要考虑的。

Knowing all the advantages of using Entity Framework, some become useless since we are recreating the DbContext each time; and more may cause overhead since the process of creating big entity models is to be considered.

对此你有何看法?

推荐答案

你是正确的单一静态实例的DbContext 一般的不推荐:

Managing Lifetime

You're correct that a single static instance of DbContext is usually not recommended:

你越使用ObjectContext的,一般就会越大。   这是因为它拥有的引用,所有它有史以来实体   知道,根本不管你质疑,添加或连接。   所以,你应该重新考虑无限期地共享相同的ObjectContext。

The more you use an ObjectContext, generally the bigger it gets. This is because it holds a reference to all the Entities it has ever known about, essentially whatever you have queried, added or attached. So you should reconsider sharing the same ObjectContext indefinitely.

这些意见直接向的DbContext ,因为它包装包装的ObjectContext 揭露的简化和更直观的API。的[看到文档]

These comments apply directly to the DbContext, because it wraps wraps ObjectContext to expose "simplified and more intuitive APIs." [see documentation]

创建上下文的开销是比较低的:

The overhead of creating the context is relatively low:

现实就是这样的成本实际上是pretty的低,主要是因为它   只涉及复制,引用,元数据从全局高速缓存   进入新的ObjectContext。一般来说,我不认为这代价值得担心......

The reality is this cost is actually pretty low, because mostly it simply involves copying, by reference, metadata from a global cache into the new ObjectContext. Generally I don’t think this cost is worth worrying about ...

有短暂的情况下工作的常用方法是把它包一个使用块:

The common way to work with a short-lived context is to wrap it in a using block:

using(DbContext context = new SomeDbContext())
{
    // Do work with context
}

要缓和与测试,您可能希望你的的DbContext 实施一些 IDbContext 接口,并创建一个工厂类 ContextFactory&LT; T&GT;其中T:IDbContext 实例化上下文。

To ease with testing, you may want to have your DbContext implement some IDbContext interface, and create a factory class ContextFactory<T> where T : IDbContext to instantiate contexts.

这可以让你轻松插拔 IDbContext 到code(即在内存中的上下文的对象嘲讽。)

This allows you to easily swap any IDbContext into your code (ie. an in-memory context for object mocking.)

  • MSDN: <一个href="http://blogs.msdn.com/b/alexj/archive/2009/05/07/tip-18-how-to-decide-on-a-lifetime-for-your-objectcontext.aspx">How决定一辈子为你的ObjectContext
  • 计算器: <一个href="http://stackoverflow.com/questions/813457/instantiating-a-context-in-linq-to-entities">Instantiating在LINQ上下文到实体
  • MSDN: How to decide on a lifetime for your ObjectContext
  • StackOverflow: Instantiating a context in LINQ to Entities

这篇关于为什么使用实体框架时,重新启动的DbContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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