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
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.
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.
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.