简体   繁体   English

实体框架 6 Create() 与 new

[英]Entity Framework 6 Create() vs new

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实例

MyEntity me = entities.myentities.Create();

will create a proxy wrapped instance of MyEntity (assuming your context is configured to create proxies)将创建MyEntity的代理包装实例(假设您的上下文配置为创建代理)

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从这里

Yes, you still need to add it.是的,您仍然需要添加它。 From the documentation of the Create method:从 Create 方法的文档中:

Creates a new instance of an entity for the type of this set.为该集合的类型创建实体的新实例。 Note that this instance is NOT added or attached to the set.请注意,此实例未添加或附加到集合。

MyEntity me = new MyEntity();

is equal to等于

MyEntity me = entities.myentities.Create();

Both of the above create a new instance of MyEntity but neither attach it to the DbSet represented by myentities.以上都创建了一个 MyEntity 的新实例,但都没有将它附加到由 myentities 表示的 DbSet。

The line线

entities.myentities.Add(me)

attaches the instance to the DbSet, though you could use Attach(me) as well.将实例附加到 DbSet,但您也可以使用Attach(me)

The "me" is required in the second example as you would be creating an instance of object without a reference to hold the object.在第二个示例中需要“me”,因为您将创建一个对象实例,而没有引用来保存该对象。

If you use entity inheritance, you can achieve nice polymorphism behaviour using Create() method because it always instantiate correct Entity (not generic one).如果使用实体继承,则可以使用 Create() 方法实现良好的多态行为,因为它始终实例化正确的实体(不是通用实体)。 Example:例子:

public DbSet GetDialDbSet(DialEnum type)
    {
        DbSet ret;
        switch (type)
        {
            default:
            case DialEnum.MAPPING_REASON:
                ret = DialMappingReasons; 
                break;

            case DialEnum.PROCESSING_INFORMATION:
                ret = DialProcessingInformation;
                break;
        }
        return ret;
    }

and polymorphism usage:和多态用法:

 DialDerived entity = (DialDerived) Db.GetDialDbSet(type).Create()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Entity Framework创建新记录 - Create a new record with Entity Framework 实体框架复杂类型与创建新实体 - Entity Framework Complex Type vs Creating new Entity 实体框架创建FirstOrDefaultAsync添加新条目 - Entity framework create FirstOrDefaultAsync add new entry 实体框架4:创建一个新的实体,保存它,没有错误,也没有插入记录吗? - Entity Framework 4: Create a new Entity, save it, no errors, and no record inserted? 实体框架创建与现有实体关系的新实体,导致尝试创建现有实体的新副本 - Entity Framework creating new entity with relationship to existing entity, results in attempt to create new copy of the existing entity 如何使用 Net core 3 和 Entity Framework 由新客户端创建新数据库? - How to create new database by new client with Net core 3 and Entity Framework? 实体框架急切加载新对象与现有对象 - Entity framework eager loading new objects vs existing objects !(ReferenceEquals())vs!=在实体框架4中 - !(ReferenceEquals()) vs != in Entity Framework 4 删除旧的Entity Framework生成的数据库后创建新的数据库 - Create new database after deleting old Entity Framework generated database 实体框架代码优先迁移始终尝试创建新数据库 - Entity framework code first migration always tries to create new database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM