简体   繁体   English

创建过程中的EF 1.0

[英]EF 1.0 during creation

I've a strange problem using EF1.0... my problem happenin' only during a creation and I didn't find anything on many forum's thread. 我在使用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

What is the real problem? 真正的问题是什么? I happens when I tried to add an Entity to another entity's list during a command creation. 我在命令创建过程中尝试将一个实体添加到另一个实体的列表时发生了。

IE: aCommand.Songs.Add(new Song() { Name = "SongName" }); IE: aCommand.Songs.Add(new Song() { Name = "SongName" });

Thank to you guys and sorry for my english ; 谢谢你们,对不起我的英语; Im from Quebec and usualy talk french! 我来自魁北克,通常会说法语!

UPDATE #1 更新#1

My line #115 in chansonwrapper (which mean "songwrapper") : 我在chansonwrapper中的第115行(表示“ songwrapper”):

aCommand.Songs.Add(new Song() { Name = "SongName" });

UPDATE #2 更新#2

Sorry, I were trying to simplify the code to write but I think its just more confusing so here is my real code : 抱歉,我正在尝试简化代码编写过程,但我认为这更加令人困惑,因此这是我的真实代码:

SongWrapper songWrapper = new SongWrapper(this.m_Context);
Song song = songWrapper.Load(request.SongId);
aCommand.Songs.Add(song);

The this.m_Context for my songWrapper ensure that my song returned in in the same context of my "aCommand" or other entity that i could load later/before. 我的songWrapper的this.m_Context确保我的歌曲在与“ aCommand”或其他我以后可以加载的实体相同的上下文中返回。 This part were alrealy tested so we know it works. 这部分经过严格测试,因此我们知道它可以工作。 One things have changed, is that we moved to "LazyLoading" what we were not using before. 一件事情发生了变化,就是我们将以前未使用的内容移到了“ LazyLoading”。 More weird, the update works fine! 更怪异,更新正常! Help meee :oP! 帮忙:oP!

This is certainly not EF 1.0. 这当然不是EF 1.0。 It must be 4.0 beta. 它必须是4.0 beta。 I think it may be a bug in lazy loading. 我认为这可能是延迟加载中的错误。 You're doing (from a back revision of your question) if( aCommand.songs.Count() > 0 . For an entity in the added state, this should not try to lazily load the songs. But it seems to be trying to do just that. My only suggestion is to work around the bug via something like: 您正在做(从您的问题的修订本开始) if( aCommand.songs.Count() > 0对于处于添加状态的实体,这不应尝试延迟加载歌曲,但似乎正在尝试我只是建议通过以下方法解决该错误:

if ((aCommand.EntityState != EntityState.Added) 
    && (aCommand.songs.Count() > 0))

...which is a bit kludgy, or turn lazy loading off for this. ...这有点麻烦,或者为此关闭了延迟加载。 If you can boil this down to a reproducible test case, you might report it to Microsoft Connect. 如果您可以将此归结为可重现的测试用例,则可以将其报告给Microsoft Connect。

By the way, you should generally prefer: 顺便说一句,您通常应该选择:

if (aCommand.songs.Any()) 

... to Count() > 0 as it's more efficient and readable. ...到Count() > 0因为它更有效,更易读。 But that's irrelevant here. 但这与这里无关。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM