c# - 已尝试附加或添加不是新实体,可能是从另一个 DataContext 加载的

标签 c# linq linq-to-sql notsupportedexception

我遇到了 NotSupportedException 问题,我得到: “已尝试附加或添加一个不是新的实体,可能是从另一个 DataContext 加载的。”

partial class SupplyOfert : Model
{
    public SupplyOfert(int id = 0)
    {
        if (id > 0)
            this.get(id);
    }

    public Supplier supplier
    {
        get
        {
            return this.Supplier;
        }
        set
        {
            this.Supplier = db.Suppliers.SingleOrDefault(s => s.id == value.id);
        }
    }

    public override bool get(int id)
    {
        SupplyOfert so = (SupplyOfert)db.SupplyOferts.SingleOrDefault(s => s.id == id).Copy(this, "Supplies");
        this._Supplies = so._Supplies;
        so._Supplies.Clear();
        if (this.id > 0)
        {
            db.Dispose();
            db = new Database();
            db.SupplyOferts.Attach(this);  // here I'm getting the exception
            return true;
        }
        else
            return false;
    }

    public override void save()
    {
        if (this.id <= 0)
            db.SupplyOferts.InsertOnSubmit(this);
        db.SubmitChanges();
    }

    public override void remove()
    {
        if (this.id > 0)
        {
            db.SupplyOferts.DeleteOnSubmit(this);
            db.SubmitChanges();
        }
    }
}

当我添加新记录时:

SupplyOfert ofert = new SupplyOfert();
ofert.price = 100;
ofert.publisher = "some publisher";
ofert.supplier = new Supplier(1);
ofert.title = "some title";
ofert.year = DateTime.Today;
ofert.save();

没关系,但是当我想更新记录时,使用:

SupplyOfert ofert = new SupplyOfert(2);
ofert.price = 220;
ofert.publisher = "new publisher";
ofert.save();

我收到“NotSupportedExceptionw 未处理”。

这是模型类:

public abstract class Model
{
    protected Database db = new Database();

    public Model(){}

    ~Model()
    {
        db.Dispose();
    }

    abstract public bool get(int id);
    abstract public void save();
    abstract public void remove();

}

复制方法如下:

public static class ObjectProperties
{
    public static object Copy(this object source, object destination, string skip = "")
    {
        if (source != null)
        {
            foreach (var sourceProperty in source.GetType().GetProperties())
            {
                foreach (var destinationProperty in destination.GetType().GetProperties())
                {
                    if (destinationProperty.Name == sourceProperty.Name && destinationProperty.Name != skip)
                        destinationProperty.GetSetMethod().Invoke(destination, new object[] { sourceProperty.GetGetMethod().Invoke(source, null) });
                }
            }
        }
        else
            destination = null;
        return source;
    }
}

数据库类它只是:

public class Database : DataClasses1DataContext

我发现,这个问题与赋值有关:

this.supplier = so.supplier;

但我不知道为什么...

最佳答案

这里的问题看起来像是您正在为每个实体创建一个单独的上下文(模型实例化了一个新的 db 副本)。那么会发生什么:

  1. 您创建对象并且它在它所附加的实体中有一个上下文对象
  2. 您使用自己的上下文类创建一个新对象并设置要更新的属性
  3. 它尝试保存并抛出实体已加载到另一个上下文(创建它的上下文)中的异常。

纠正这个问题的第一步是不要在实体内部创建新的上下文。

关于c# - 已尝试附加或添加不是新实体,可能是从另一个 DataContext 加载的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549496/

相关文章:

c# - 如何使用 Entity Framework 和 LINQ to SQL 以及 LinqKit PredicateBuilder 或 IdeaBlade DevForce 过滤相关数据

c# - 如何延迟 Unity 和 C# 中的方法?

linq - 如何在 F# 中执行 linq-to-IEnumerable 风格的连接

c# - 使用 LINQ 减去表中的列

c# - LINQ to SQL 按 List<int> 过滤

c# - 我什么时候可以处理我的 DataContext?

c# - 带有 Google 的 ASP 网络文本框,如下拉搜索值

c# - C# : are they faster than iterators, 中的纤程,有人使用过它们吗?

vb.net - 在 VB.NET 中使用 LINQ 从数据表填充列表(字符串)

c# - 需要简单的 linq