试图附加或添加一个并非新实体,可能是从另一个DataContext加载的



我遇到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;

但我不知道为什么。。。

这里的问题看起来像是为每个实体创建了一个单独的上下文(Model实例化了一个新的db副本)。所以发生的是:

  1. 您创建了对象,它在附着到的实体中有一个上下文对象
  2. 您使用自己的上下文类创建一个新对象,并设置要更新的属性
  3. 它试图保存并抛出一个异常,即实体已经加载到另一个上下文中,也就是创建实体的上下文中

纠正这种情况的第一步是不要在实体内创建新的上下文。

我同意@Matthew

此外,我想建议dbcontexts应该在使用块中:

using(var db = new Database())
{
    // do something with db but not lazy loading
}

这对于数据库上下文

来说也是一个不错的阅读

最新更新