我已经尝试了一段时间来调试这个程序,并以不同的方式表达了我想要做的事情,但是我没有运气。我对实体框架不太了解,我从另一个已经不在的开发人员那里继承了这段代码,所以我对模型不太了解。也许有人至少能让我指向正确的方向。这是我的密码:
//Append note to project notes too
task.TPM_PROJECTVERSIONReference.Load();
TPM_PROJECTVERSIONNOTES newNote = new TPM_PROJECTVERSIONNOTES();
newNote.TS = System.DateTime.Now;
newNote.TPM_USER = SessionHandler.LoginUser;
newNote.NOTES = task.NOTES;
newNote.PROJECTID = this.ProjectId;
newNote.VERSIONID = this.VersionId;
task.TPM_PROJECTVERSION.TPM_PROJECTVERSIONNOTES.Add(newNote); //<-- Exception我有“任务”,它指向它所属的项目版本。这些项目有一堆笔记。我想给任务的项目添加一个新的注释。但是,在最后一行中,我得到了一个例外:
System.InvalidOperationException未被用户代码处理
不能定义两个对象之间的Message=The关系,因为它们连接到不同的ObjectContext对象。
Source=System.Data.Entity
这看起来应该是件很简单的事。这样做不对吗?谢谢!
编辑:
好的,我已经从函数中删除了所有其他代码,并创建了一个全新的上下文。下面是从开始到结束的整个函数:
//Append note to project notes too
using (TPMEntities context = new TPMEntities(General.EntityName()))
{
TPM_PROJECTVERSIONNOTES newNote = context.TPM_PROJECTVERSIONNOTES.CreateObject();
newNote.TS = System.DateTime.Now;
newNote.TPM_USER = SessionHandler.LoginUser;
newNote.NOTES = this.txtTaskNotes.Text;
TPM_PROJECTVERSION version = (from pv in context.TPM_PROJECTVERSION
where pv.PROJECTID == this.ProjectId && pv.VERSIONID == this.VersionId
select pv).First();
version.TPM_PROJECTVERSIONNOTES.Add(newNote);
context.SaveChanges();
}不过,我也有同样的例外:
System.InvalidOperationException was unhandled by user code
Message=The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
Source=System.Data.Entity我只是不明白为什么会有两种情境?我在代码中的每一个地方都使用“上下文”。实体框架如何使简单地向我的数据库中添加一行变得如此困难?
发布于 2011-10-07 00:55:01
问题就在
newNote.TPM_USER = SessionHandler.LoginUser;LoginUser是使用另一个上下文创建的。您正在将其附加到context。有几种方法可以解决这个问题。
将LoginUser从创建的上下文中分离出来。
sessionContext.Detach(SessionHandler.LoginUser);然后在你的方法里面
using (TPMEntities context = new TPMEntities(General.EntityName()))
{
context.Attach(SessionHandler.LoginUser);
TPM_PROJECTVERSIONNOTES newNote = context.TPM_PROJECTVERSIONNOTES.CreateObject();
newNote.TS = System.DateTime.Now;
newNote.TPM_USER = SessionHandler.LoginUser;
newNote.NOTES = this.txtTaskNotes.Text;
TPM_PROJECTVERSION version = (from pv in context.TPM_PROJECTVERSION
where pv.PROJECTID == this.ProjectId && pv.VERSIONID == this.VersionId
select pv).First();
version.TPM_PROJECTVERSIONNOTES.Add(newNote);
context.SaveChanges();
}或者如果您映射了标量外键属性newNote.TPM_USER_ID
using (TPMEntities context = new TPMEntities(General.EntityName()))
{
TPM_PROJECTVERSIONNOTES newNote = context.TPM_PROJECTVERSIONNOTES.CreateObject();
newNote.TS = System.DateTime.Now;
newNote.TPM_USER_ID = SessionHandler.LoginUser.ID;
newNote.NOTES = this.txtTaskNotes.Text;
TPM_PROJECTVERSION version = (from pv in context.TPM_PROJECTVERSION
where pv.PROJECTID == this.ProjectId && pv.VERSIONID == this.VersionId
select pv).First();
version.TPM_PROJECTVERSIONNOTES.Add(newNote);
context.SaveChanges();
}发布于 2011-10-06 17:23:35
尝试将Web.Config连接字符串设置为包含MultipleActiveResultSets=True。
发布于 2011-10-06 19:27:33
经过近4个小时的努力,我能得到的唯一解决方案就是这个。显然,将新数据插入数据库是实体框架的一个高级特性,需要深入了解其内部工作原理。
context.ExecuteStoreQuery<object>("INSERT INTO TPM_PROJECTVERSIONNOTES (NOTES, PROJECTID, VERSIONID, USERID, TS) VALUES (:notes, :projId, :versionId, :userId, :timestamp)",
new OracleParameter("notes", this.txtTaskNotes.Text),
new OracleParameter("projId", this.ProjectId),
new OracleParameter("versionId", this.VersionId),
new OracleParameter("userId", SessionHandler.LoginUser.USERID),
new OracleParameter("timestamp", DateTime.Now));哦,好吧,至少很快。
https://stackoverflow.com/questions/7678004
复制相似问题