尝试将新的EF4实体附加到ObjectContext时出现问题,其entitycollectionentityisalreadyattached这有点难以解释,请耐心等待。我有一个正在慢慢杀死我的ASP.NETMVC2项目,我正在尝试获取表单数据并将其转换为实体以根据情况创建或更新。最相关的部分(伪代码):实体游戏标量属性EntityCollection平台基本工作流程是:表单数据->模型绑定到DTO->使用AutoMapper将DTO映射到EF4实体。一切正常,只有一个例外——我需要使用DTO中包含的原始整数索引数据创建或更新游戏实体的PlatformsEntityCollection。所以,这是我一直在尝试的,这是行不通的:_gameRepository=gameRepository;_newsRepository=新闻资料库;Mapper.CreateMap().BeforeMap((s,d)=>{if(d.Platforms.Count>0){Platform[]existing=d.Platforms.ToArray();foreach(varplatinexisting){d.Platforms.Remove(plat);}}foreach(varplatIdins.PlatformIDs){varnewPlat=_gameRepository.GetPlatform(platId);d.Platforms.Add(newPlat);//dest.BoxArtPath,opt=>opt.Ignore()).ForMember(dest=>dest.IndexImagePath,opt=>opt.Ignore()).ForMember(dest=>dest.Cons,opt=>opt.MapFrom(src=>String.Join("|",src.Cons))).ForMember(dest=>dest.Pros,opt=>opt.MapFrom(src=>String.Join("|",src.Pros))).ForMember(dest=>dest.LastModified,opt=>opt.UseValue(DateTime.Now)).ForMember(dest=>dest.Platforms,opt=>opt.Ignore());具体来说,我在上面突出显示的行中遇到异常:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象一些研究告诉我这是可以预料的,因为我的新游戏对象没有与之关联的ObjectContext,并且空上下文被认为是一个单独的上下文。有关详细信息,请参阅JulieLerman的简短说明。好吧,我是菜鸟,我想我可以用ObjectContext注册我的游戏,一切都会得到修复。所以,我尝试了:Gamegame=newGame();_gameRepository.RegisterGame(游戏);RegisterGame简单地:publicvoidRegisterGame(Gamegame){_siteDB.Games.AddObject(game);不幸的是,这并没有启动。在同一点抛出相同的异常。因此,看起来我必须将每个平台的EntityKey添加到我的EntityCollection中。唯一的问题是,我不知道该怎么做。那么,有什么想法吗?编辑:一种改进。我尝试仅添加平台实体的EntityKeys,如下所示:Mapper.CreateMap().BeforeMap((s,d)=>{if(d.Platforms.Count>0){Platform[]existing=d.Platforms.ToArray();foreach(varplatinexisting){d.Platforms.Remove(plat);}}foreach(varplatIdins.PlatformIDs){varnewPlat=_gameRepository.GetPlatform(platId);d.Platforms.Add(newPlatform{EntityKey=newPlat.EntityKey});}})它删除了“两个不同的ObjectContexts”异常。问题是,如果我尝试向我的上下文添加或附加一个新的游戏实体,我会收到以下异常:ObjectStateManager中已存在具有相同键的对象。ObjectStateManager无法跟踪具有相同键的多个对象。不幸的是,异常没有指定它是哪个对象,但我假设它是Platform对象之一,因为它们已经存在并且我只是试图在两者之间创建新的关系。因此,问题仍然存在:如何创建新实体并使用现有实体填充其EntityCollection属性?我不可能是唯一想要创建一个新实体并在它与现有实体之间创建新的多对多关系的人,对吗?试试这个:foreach(varplatIdins.PlatformIDs){Platformfromp=newPlatform{Id=platId};context.Attach(p)d.Platforms.Add(p);您不必加载实体来建立关系。您只需要一个具有正确Id的假人(您已经拥有)。虚拟必须附加到上下文。我通过使用JulieLerman的原始解决方案让它工作。我不必使用我原来的DTO预解决方案分离/重新连接我的平台,所以我认为我不需要在这里。无论如何,看起来我需要对如何处理ObjectContext做更多的研究。我最初通过简单地创建我自己笨拙的静态映射器来解决这个问题(以及我在AutoMapper和EF4多对多图形方面遇到的其他问题)。它很丑陋而且一点也不抽象,但它确实有效。随着AutoMapper2.0的发布,我决定看看是否可以按照我想要的方式完成所有工作。令人惊讶的是,我在AutoMapper代码中遇到了与最初相同的“DifferentObjectContexts”异常。它在我自己的方法中无一例外地工作,但在AutoMapper中却没有。奇怪,代码基本一样。AutoMapper地图:Mapper.CreateMap().BeforeMap((s,d)=>{if(d.Platforms!=null&&d.Platforms.Count>0){varoldPlats=d.Platforms.ToArray();foreach(varoldPlatinoldPlats){d.Platforms.Remove(oldPlat);}}foreach(varplatIdins.PlatformIDs){varplat=_gameRepository.GetPlatform(platId);d.Platforms.Add(plat);}})。ForMember(dest=>dest.Platforms,opt=>opt.Ignore()).ForMember(dest=>dest.BoxArtPath,opt=>opt.Ignore()).ForMember(dest=>dest.IndexImagePath,opt=>opt.Ignore()).ForMember(dest=>dest.Cons,opt=>opt.MapFrom(src=>string.Join("|",src.Cons))).ForMember(dest=>dest.Pros,opt=>opt.MapFrom(src=>string.Join("|",src.Pros))).ForMember(dest=>dest.LastModified,opt=>opt.UseValue(DateTime.Now));我自己的投影仪:publicstaticGameMapFromEditModelToGame(IGameRepositoryrepo,AdminGameViewModelformData,GamenewGame){newGame.GameID=formData.GameID;newGame.GameTitle=formData.GameTitle;新游戏。GenreID=formData.GenreID;newGame.LastModified=DateTime.Now;newGame.ReviewScore=(短)formData.ReviewScore;newGame.ReviewText=formData.ReviewText;newGame.Cons=String.Join("|",formData.Cons);newGame.Pros=String.Join("|",formData.Pros);newGame.Slug=formData.Slug;if(newGame.Platforms!=null&&newGame.Platforms.Count>0){varoldPlats=newGame.Platforms.ToArray();foreach(varoldPlatinoldPlats){newGame.Platforms.Remove(oldPlat);}}foreach(formData.PlatformIDs中的varplatId){varplat=repo.GetPlatform(platId);newGame.Platforms.Add(平台);}返回新游戏;我只能猜测游戏中存在一些奇怪的范围问题,但我认为这是一个有趣的(阅读:令人沮丧的)问题无法判断这是由于EF4本身造成的,还是我正在尝试将其与AutoMapper集成一起。以上就是C#学习教程:试图给ObjectContext附加一个新的EF4实体,同时附加它的实体集合实体有问题注意——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
