无法定义关系,因为它们附加到不同的 ObjectContext 对象

Relationship cannot be defined because they are attached to different ObjectContext objects

我不知道我错过了什么,因为无论页面如何调用,都会执行相同的 page_load,但是当我退出页面而不保存任何内容,然后返回到页面,似乎我的上下文对象无法像我在退出页面之前保存更改时那样检索使用的值,导致我在尝试从那里保存时出现此异常:

无法设置两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。

这是我在页面加载中的内容:

public partial class FraisDeplacement : System.Web.UI.UserControl
{
    BLL.SessionContext context;
    DAL.DBObjects.VersionDemande versionDemande;


    protected void Page_Load(object sender, EventArgs e)
    {
        this.context = (BLL.SessionContext)Session["sessionContext"];

        this.Page.LoadComplete += new EventHandler(Page_LoadComplete);

        versionDemande = (DAL.DBObjects.VersionDemande)Session["versionDemande"];

    }

}

我也有这个问题,你可能需要这两个功能之一:

context.DBAccess.Attach(versionDemande);

或者这个

versionDemande = this.context.DBAccess.ApplyCurrentValues<DAL.DBObjects.VersionDemande>("VersionDemande", versionDemande);

根据您的对象在页面上的表现created/retreived,我建议您首先将它们放入 try catch 中,这将帮助您了解它们失败时发生的情况。