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

后端 未结 3 599
迷失自我
迷失自我 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.

提交回复
热议问题