Problems updating w/ EF4 repo & MVC2 - can't update many-to-many graph

左心房为你撑大大i 提交于 2019-11-29 17:52:10

Kevin, oh this is a little complex and forces you back to EFv1 patterns because with M:M you don't have foreign keys to lean on and you are stuck with having objects.

WHen you add a game, you do want the relationship (i.e. a row in the join table) to be added but you don't want the platform to be added since it's just a reference.

I haven't actually done this but I think it would be easier if you could break it apart and then rebuild the platforms collection once the game is attached and marked added. Otherwise if you add the whole graph, everything gets marked added.

Problem with EF is that if you attach game you will get the related stuff attached too. There might be a cleaner pattern but my thought is to detach the platforms from the game, attach the game to the context, mark it as added. Then I would attach the platforms to the context. They will be "unchanged". Then add them to the games.platform collection. The platforms will still be unchanged but the : will be understood.

You may have tried that. I'd have to do it myself and watch the entity state of everything as I go along to see for sure what's happening. The key is that EF needs to track that a relationship has been added and that's new (and will result in a row in the join table being added) but understand that Platforms are not new.

hth julie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!