本文介绍了LINQ到SQL - 保存一个实体,而无需创建一个新的DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误

无法用钥匙是已在使用添加的实体

当我尝试保存项目

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Item item)
{
  Global.DataContext.Items.Attach(item);
  Global.DataContext.SubmitChanges();

  return View(item);
}

这是因为我不能将项目连接到全球的DataContext。

That's because I cannot attach the item to the global DataContext.

是否可以保存项目,而无需创建一个新的DataContext,而无需到项目的每个字段手动分配?

Is it possible to save an item without creating a new DataContext, and without having to assign each field of the item manually?

(我是很新的LINQ)

(I am very new to LINQ)

编辑:我意识到静态的DataContext会造成问题由于下面的意见,现在是这样

I realised static DataContext would cause problems thanks to the comments below, it is now like this

public static AbcDataContext DataContext
{
  get
  {
    if (!HttpContext.Current.Items.Contains("DataContext"))
      HttpContext.Current.Items["DataContext"] = new AbcDataContext(ConnectionString);
    return (AbcDataContext)HttpContext.Current.Items["DataContext"];
  }
}

(雷克斯可能不同意,但我不能打扰改变整个code的时刻 - 可能会更高版本)

(Rex might not agree to that, but I can't be bothered changing the whole code at the moment - may be later)

推荐答案

DataContext的讨论。注意:我不评论你的code。

DataContext discussion. Note I'm not commenting on your code.

DataContexts实施的IDisposable ,因此,你应该处理的数据上下文,当它不再需要。您的网站的作品不够好发展,但在生产中你会得到钉。你还不如做是正确的之前,你的code变得太根深蒂固,改变这将是一个很大的麻烦。充其量你只是养成的坏习惯。

DataContexts implement IDisposable, and therefore you should be disposing of the data context when it's no longer needed. Your website works well enough in development, but in production you will get nailed. You might as well do it right before your code gets too entrenched and changing it will be a big hassle. At best you'll just develop bad habits.

要你写什么更好的选择是有管理的寿命为你自己的控制器基类。

A better alternative to what you've written is to have your own controller base class that manages the lifetime for you.

public class MyBaseController : System.Web.Mvc.Controller
{
    private AbcDataContext abcDataContext;

    protected AbcDataContext DataContext
    {
        get
        {   // lazy-create of DataContext
            if (abcDataContext == null)
                abcDataContext = new AbcDataContext(ConnectionString);

            return abcDataContext;
        }
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        if (disposing)
        {
            if( abcDataContext != null )
                abcDataContext.Dispose();
        }
    }
}

这允许你做

public class MyController : MyBaseController
{
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Item item)
    {
        DataContext.Items.Attach(item);
        DataContext.SubmitChanges();

        return View(item);
    }
}

虽然这个作品,我个人觉得这很令人讨厌和棘手。

While this works, I personally find it can get annoying and tricky.

最好的:如果你要遵循MVC为你应该,你应该完全填充模式,而不是依靠延迟加载实体。实现这一目标的最好办法是摆脱你的DataContext的,只要你能。

Best: If you're going to follow MVC as you're supposed to, you should populate the model completely and not rely on lazy-loading entities. The best way to achieve this is to get rid of your DataContext as soon as you can.

通常情况下,我们执行这一时,透过以下模式的code级:

Typically, we enforce this at a code level via the following pattern:

using( var dc = new AbcDataContext(ConnectionString))
{
    var itemUpdater = new ItemUpdater(dc);
    item = itemUpdater.Update(item);
}
return View(item);

我们的想法是,你会得到一个的ObjectDisposedException 如果你的观点试图通过延迟加载得到任何额外的数据。

The idea is that you will get an ObjectDisposedException if your view attempts to get any additional data via lazy-loading.

这篇关于LINQ到SQL - 保存一个实体,而无需创建一个新的DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:12