的EntityFramework增加新对象到集合 [英] EntityFramework adding new object to a collection

查看:164
本文介绍了的EntityFramework增加新对象到集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这个问题已经回答过,如果是那样的话我会AP preciate如果你们点我在正确的方向。

Probably this question has been answered before, if that's the case I would appreciate if you guys point me in the right direction.

我想知道,当一个新的对象被添加到的EntityFramework集合会发生什么。

I would like to know what happens when a new object is added to an EntityFramework collection.

更多precisely,我想知道如果为了添加新对象的整个集合加载到内存

More precisely, I'd like to know if in order to add the new object the whole collection is loaded into memory

例如:

public class MyContext : DbContext
{
    public DbSet<Assignment> Assignments { get; set; }
}

public class SomeClass
{
    public void AddAssignment(Assignment assignment)
    {
        var ctx = new MyContext();

        ctx.Assignments.Add(assignment);

        ctx.SaveChanges();
    }
}

是否所有转让的记录都被加载到内存中只是进行简单的插入???

Do all the assignment records have to be loaded into memory just to perform a simple insert???

推荐答案

简而言之:整个实体集的空载过程

In short: no load process of entire entity collection.

ADDOBJECT()方法被用于添加不存在于数据库中新创建的对象。当 ADDOBJECT()时,一个临时的的EntityKey 生成和的EntityState设置为补充,如图所示如下:

The AddObject() method is used for adding newly created objects that do not exist in the database. When AddObject() is called, a temporary EntityKey is generated and the EntityState is set to 'Added', as shown below:

context.SaveChanges()被调用时,EF 4.0继续并插入记录到数据库中。需要注意的是实体框架转换code到查询数据库理解和处理所有数据交互和低级别的细节。在code也注意到上面,我们正在访问数据对象和属性。

When context.SaveChanges() is called, EF 4.0 goes ahead and inserts the record into the database. Note that Entity Framework converts the code into queries that the database understand and handles all data interaction and low-level details. Also notice in the code above, that we are accessing data as objects and properties.

在执行了code,你可以继续前进,物理验证数据库中的记录。

After you have executed the code, you can go ahead and physically verify the record in the database.

这篇关于的EntityFramework增加新对象到集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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