Is is possible to check if an object is already attached to a data context in Entity Framework?

后端 未结 5 962
清歌不尽
清歌不尽 2020-11-28 04:26

I am getting the following error when trying to attach an object that is already attached to a given context via context.AttachTo(...):

A

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 05:08

    A simpler approach is:

     bool isDetached = context.Entry(user).State == EntityState.Detached;
     if (isDetached)
         context.Users.Attach(user);
    

提交回复
热议问题