【问题标题】:Relationship cannot be defined because they are attached to different ObjectContext objects无法定义关系,因为它们附加到不同的 ObjectContext 对象
【发布时间】:2015-05-19 20:17:03
【问题描述】:

我不知道我错过了什么,因为无论页面如何调用,都会执行相同的 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"];

    }

}

【问题讨论】:

    标签: c# asp.net entity-framework-4


    【解决方案1】:

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

    context.DBAccess.Attach(versionDemande);
    

    或者这个

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

    根据您的对象在页面上的创建/检索方式,我建议您首先将它们放入 try catch,如果它们失败,它将帮助您查看发生了什么。

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多