![](/img/trans.png)
[英]The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework
[英]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);
根據您在頁面上創建/撤消對象的方式,我建議您首先將它們放入try catch中,這將幫助您查看失敗后的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.