实体框架6 Create()vs new [英] Entity Framework 6 Create() vs new

查看:114
本文介绍了实体框架6 Create()vs new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  MyEntity me = new MyEntity(); 
entities.myentities.Add(me);

vs

  MyEntity me = entities.myentities.Create(); 

我还需要在第二个例子中添加me吗?如果是这样,有什么优势,一种还是另一种?



非常感谢!

解决方案

  MyEntity me = new MyEntity(); 

将创建一个新的实例MyEntity / p>

  MyEntity me = entities.myentities.Create(); 

将创建一个代理包装的实例 MyEntity (假设您的上下文配置为创建代理)


此代理覆盖实体的某些虚拟属性,以插入钩子,以便在执行操作时自动执行操作属性被访问。例如,此机制用于支持延迟关系的加载。


从这里


What is the difference between adding an entity in these two ways?

MyEntity me = new MyEntity();
entities.myentities.Add(me);

vs

MyEntity me = entities.myentities.Create();

Do I still need to add "me" in the second example? If so, is there some sort of advantage, one way or the other?

Many thanks!

解决方案

MyEntity me = new MyEntity();

will create a new instance of MyEntity

MyEntity me = entities.myentities.Create();

will create a proxy wrapped instance of MyEntity (assuming your context is configured to create proxies)

This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed. For example, this mechanism is used to support lazy loading of relationships.

from here

这篇关于实体框架6 Create()vs new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆