[英]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.