Problems trying to attach a new EF4 entity to ObjectContext while its entity collection entities are already attached

后端 未结 3 598
迷失自我
迷失自我 2021-01-17 03:05

This is somewhat complicated to explain, so please bear with me.

I have an ASP.NET MVC 2 project that is slowly killing me in which I\'m trying to t

相关标签:
3条回答
  • 2021-01-17 03:33

    Just try this:

    foreach (var platId in s.PlatformIDs)
    {
        Platfrom p = new Platform { Id = platId };
        context.Attach(p) 
        d.Platforms.Add(p);
    }
    

    You don't have to load the entity to make a relation. You just need a dummy with correct Id (which you already have). The dummy must be attached to the context.

    0 讨论(0)
  • 2021-01-17 03:45

    I had originally solved this (and other problems I was having with EF4 many-to-many graphs in conjunction with AutoMapper) by simply creating my own cludgy static mapper. It's ugly, not at all abstract, but it works. With the release of AutoMapper 2.0, I decided to see if I could finally get everything to work the way I want.

    Amazingly, I got the same "different ObjectContexts" exception in the AutoMapper code as I did originally. It works without an exception in my own method, but not in AutoMapper. Odd, seeing as the code is essentially the same.

    AutoMapper map:

    Mapper.CreateMap<AdminGameViewModel, Game>()
        .BeforeMap((s, d) =>
        {
            if (d.Platforms != null && d.Platforms.Count > 0)
            {
                var oldPlats = d.Platforms.ToArray();
    
                foreach (var oldPlat in oldPlats)
                {
                    d.Platforms.Remove(oldPlat);
                }
            }
    
            foreach (var platId in s.PlatformIDs)
            {
                var plat = _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));
    

    My own mapper:

    public static Game MapFromEditModelToGame(IGameRepository repo, AdminGameViewModel formData, Game newGame)
    {
        newGame.GameID = formData.GameID;
        newGame.GameTitle = formData.GameTitle;
        newGame.GenreID = formData.GenreID;
        newGame.LastModified = DateTime.Now;
        newGame.ReviewScore = (short)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)
        {
            var oldPlats = newGame.Platforms.ToArray();
    
            foreach (var oldPlat in oldPlats)
            {
                newGame.Platforms.Remove(oldPlat);
            }
        }
    
        foreach (var platId in formData.PlatformIDs)
        {
            var plat = repo.GetPlatform(platId);
            newGame.Platforms.Add(plat);
        }
    
        return newGame;
    }
    

    I can only guess that there's some odd scope issue in play, but I think it's an interesting (read: frustrating) problem. Can't tell if it's due to EF4 itself, or my attempt to use it with AutoMapper.

    0 讨论(0)
  • 2021-01-17 03:46

    I got it to work by using Julie Lerman's original solution. I didn't have to detach/re-attach my platforms with my original, pre-DTO solution, so I thought I didn't need to here. In any event, it looks like I need to do more research on how to handle the ObjectContext.

    0 讨论(0)
提交回复
热议问题