简体   繁体   English

InvalidOperationException由于存在不同的ObjectContext对象

[英]InvalidOperationException because of different ObjectContext objects

I'm facing an issue I don't really understand with Entity Manager (C#). 我遇到了一个我对实体管理器(C#)不太了解的问题。 I'm kinda new at this stuff so please bear with my noobness ^^ 我对这个东西有点陌生,所以请忍受我的傻瓜^^

I'm trying to create an instance of UserQuestion (which makes the link in the DataBase between the tables UserQuestionnaire and Question ; UserQuestionnaire linking a User and a Questionnaire). 我正在尝试创建UserQuestion的实例(这将在数据库中的表UserQuestionnaire和Question之间建立链接; UserQuestionnaire将User和Questionnaire链接在一起)。

So here is the code in question : 所以这是有问题的代码:

public static void Insert_Resultat_Question_BDD(Question q, double resultat) {
     DataSourceContainer bdd = new DataSourceContainer(); // initializes the Context
     User user = BLL.Users.Get_User_Connecté(); // Gets the connected user
     Questionnaire questionnaire = BLL.Questionnaires.Get_Questionnaire_En_Cours(); // Gets the questionnaire being taken
     UserQuestionnaire user_questionnaire = bdd.UserQuestionnaireSet.FirstOrDefault(i => i.User.id == user.id && i.Questionnaire.id == questionnaire.id); // Get the UserQuestionnaire object for the user and questionnaire above
     UserQuestion uq = new UserQuestion {  score = resultat * q.bareme, bareme = q.bareme, Question = q, UserQuestionnaire = user_questionnaire }; // Creates the object
     bdd.AddToUserQuestionSet(uq); // Add it to the table
     bdd.SaveChanges(); // Save
 }

I'm getting an error when creating the UserQuestion, because the UserQuestionnaire = user_questionnaire statement launches an InvalidOperationException because : "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." 创建UserQuestion时出现错误,因为UserQuestionnaire = user_questionnaire语句启动了InvalidOperationException,因为: “两个对象之间的关系无法定义,因为它们附加到不同的ObjectContext对象上。”

What I don't understand is that I only use 1 context ! 我不明白的是,我只使用1个上下文! (First line in the code I shared) (我共享的代码的第一行)

Can someone help me through this ? 有人可以帮我吗? I don't really know what more to do :s 我真的不知道该怎么办

Thanks ! 谢谢 !

听起来像BLL.Questionnaires.Get_Questionnaire_En_Cours()使用其自己的ObjectContext。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法定义关系,因为它们已附加到不同的ObjectContext对象 - Relationship cannot be defined because they are attached to different ObjectContext objects “无法定义两个对象之间的关系,因为它们被附加到不同的ObjectContext对象。” -错误 - 'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error 引发异常:因为两个对象附​​加到不同的ObjectContext对象,所以无法定义它们之间的关系 - Exception thrown: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects EF4错误:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象Entity Framework - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework 无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 实体框架v4 - 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - Entity Framework v4 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们已附加到不同的ObjectContext对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 实体框架:“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。” - Entity Framework: “The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.” 无法找出哪些两个对象附​​加到不同的ObjectContext对象 - Cannot figure out which two objects are attached to different ObjectContext objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM