试图附加或添加一个不是新的实体,可能是从另一个DataContext加载的 [英] An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext

查看:110
本文介绍了试图附加或添加一个不是新的实体,可能是从另一个DataContext加载的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a problem with NotSupportedException, I'm getting: "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another 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();

可以,但是当我想更新记录时,请使用:

It's ok, but when I want to update record, using:

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

我收到未处理NotSupportedExceptionw".

I'm getting "NotSupportedExceptionw was unhandled".

这是Model类:

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


我发现,这个问题与作业有关:


I've found out, that this problem is connected with assignment:

this.supplier = so.supplier;

但是我不知道为什么...

But I don't know why...

推荐答案

这里的问题似乎是您正在为每个实体创建一个单独的上下文(Model具有实例化的db的新副本).所以发生的是:

The issue here looks like you are creating a separate context with each entity (Model has a new copy of db instantiated). So what happens is:

  1. 您创建了该对象,并且该对象在其所连接的实体中具有一个上下文对象
  2. 您可以使用自己的上下文类创建一个新对象,并设置要更新的属性
  3. 它试图保存并抛出一个异常,表明该实体已经在另一个上下文(创建该上下文的上下文)中加载.

纠正此问题的第一步是不在实体内部创建新的上下文.

The first step to correcting this is to not create a new context inside the entity.

这篇关于试图附加或添加一个不是新的实体,可能是从另一个DataContext加载的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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