Peu*_*uge 5 c# entity-framework
有没有人知道以下陈述的差异(如果有的话)?
_context.AddObject(user);
_context.Users.AddObject(user);
Run Code Online (Sandbox Code Playgroud)
以及
_context.Attach(user);
_context.Users.Attach(user);
Run Code Online (Sandbox Code Playgroud)
谢谢
编辑
对不起有些困惑:
我知道AddObject和Attach之间的区别,我的意思是你使用AddObject的方式有什么不同
_context.AddObject(user);
_context.Users.AddObject(user);
Run Code Online (Sandbox Code Playgroud)
一个 ObjectContext 可以有多个具有相同架构的集,因此通常最好使用显式集。IE_context.Users.AddObject(user);
来自MSDN:
在 .NET Framework 版本 4 中,我们建议您使用 ObjectSet 对象上的方法来执行创建、读取、删除、附加和更新操作。ObjectSet 派生自 ObjectQuery,因此它也可以用作查询对象。
在从 .NET Framework 版本 4 开始的版本中,您可以使用 ObjectSet 上定义的以下方法,而不是 ObjectContext 上定义的等效方法:
Run Code Online (Sandbox Code Playgroud)AddObject Attach ApplyCurrentValues ApplyOriginalValues DeleteObject Detach