我在使用EF1.0时遇到了一个奇怪的问题。。。我的问题只发生在一次创作中,在很多论坛的帖子上我都没有找到任何东西。
> System.InvalidOperationException: The source query for this EntityCollection or EntityReference cannot be returned when the related object is in either an added state or a detached state and was not originally retrieved using the NoTracking merge option.
at System.Data.Objects.DataClasses.RelatedEnd.CreateSourceQuery[TEntity](MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.CreateSourceQuery()
at Microsoft.Data.EFLazyLoading.LazyEntityCollection`1.CreateSourceQuery()
at Microsoft.Data.EFLazyLoading.LazyEntityCollection`1.LoadStubs()
at mptradModel.ContextObjects.ChansonWrapper.AttachEntities(Chanson chanson, ChansonRequest request) in (SolutionDir)\ProjectWrapper\ContextObjects\ChansonWrapper.cs:line 115
真正的问题是什么?当我在命令创建过程中尝试将一个实体添加到另一个实体的列表时,会发生这种情况。
aCommand.Songs.Add(new Song() { Name = "SongName" });
谢谢你们,对不起我的英语;我来自魁北克,通常说法语!
更新#1
添加(新歌(){Name=“SongName”});
更新#2
SongWrapper songWrapper = new SongWrapper(this.m_Context);
Song song = songWrapper.Load(request.SongId);
aCommand.Songs.Add(song);
songWrapper的this.m_上下文确保我的歌曲返回到与我的“aCommand”或其他实体相同的上下文中,我可以稍后/之前加载这些实体。这部分已经过测试,所以我们知道它是有效的。有一件事已经改变了,那就是我们开始“懒散地加载”以前没有用过的东西。更奇怪的是,更新工作正常!救命米:啊!