Entity Framework Tutorial Basics(22):Disconnected Entities
Disconnected Entities:
Before we see how to perform CRUD operation on disconnected entity graph, let's see how to associate disconnected entity graph with the new context instance.
There are two things we need to do when we get a disconnected entity graph or even a single disconnected entity. First, we need to attach entities with the new context instance and make context aware about these entities. Second, set appropriate EntityStates to these entities manually because the new context instance doesn't know anything about the operations performed on the disconnected entities, so the new context cannot apply the appropriate EntityState.
The following figure illustrates this process.

Entity Framework API provides some important methods that attaches disconnected entities to the new context and also set EntityStates to all the entities of an entity graph.
DbSet.Add():
DbSet.Add() method attaches the entire entity graph to the new context and automatically applies Added entity state to all the entities.
Consider the following example code.
//disconnected entity graph
Student disconnectedStudent = new Student() { StudentName = "New Student" };
disconnectedStudent.StudentAddress = new StudentAddress() { Address1 = "Address", City = "City1" }; using (var ctx = new SchoolDBEntities())
{
//add disconnected Student entity graph to new context instance - ctx
ctx.Students.Add(disconnectedStudent); // get DbEntityEntry instance to check the EntityState of specified entity
var studentEntry = ctx.Entry(disconnectedStudent);
var addressEntry = ctx.Entry(disconnectedStudent.StudentAddress); Console.WriteLine("Student EntityState: {0}",studentEntry.State); Console.WriteLine("StudentAddress EntityState: {0}",addressEntry.State);
}
Student EntityState: Added
StudentAddress EntityState: Added
As per the above example code, we add disconnectedStudent entity graph using ctx.Students.Add method. Here, parent entity is Student, so we have added a whole entity graph in Students DbSet. We then get the DbEntityEntry instance for Student and StudentAddress entities to check the state of each entity. As you can see in the output, both entities have Added state.
Thus, use Add method of parent DbSet entity to attach the entire entity graph to the new context instance with Added state to each entity. This will execute insert command for all the entities, which will insert new rows in the appropriate database table.
DbSet.Attach():
DbSet.Attach method attaches a whole entity graph to the new context with Unchanged entity state.
Consider the following example code.
//disconnected entity graph
Student disconnectedStudent = new Student() { StudentName = "New Student" };
disconnectedStudent.StudentAddress = new StudentAddress() { Address1 = "Address", City = "City1" }; using (var ctx = new SchoolDBEntities())
{
//attach disconnected Student entity graph to new context instance - ctx
ctx.Students.Attach(disconnectedStudent); // get DbEntityEntry instance to check the EntityState of specified entity
var studentEntry = ctx.Entry(disconnectedStudent);
var addressEntry = ctx.Entry(disconnectedStudent.StudentAddress); Console.WriteLine("Student EntityState: {0}",studentEntry.State); Console.WriteLine("StudentAddress EntityState: {0}",addressEntry.State);
}
Student EntityState: Unchanged
StudentAddress EntityState: Unchanged
As per the above code, we can attach disconnected entity graph using DbSet.Attach method. This will attach the entire entity graph to the new context with Unchanged entity state to all entities.
Thus, Attach method will only attach entity graph to the context, so we need to find the appropriate entity state for each entity and apply it manually.
DbContext.Entry():
Entry method of DbContext returns DbEntityEntry instance for a specified entity. DbEntityEntry can be used to change the state of an entity.
DbContext.Entry(disconnectedEntity).state = EntityState.Added/Modified/Deleted/Unchanged
This method attaches a whole entity graph to the context with specified state to the parent entity and set the state of other entities, as shown in the following table.
| Parent Entity State | Entity State of child entities |
|---|---|
| Added | Added |
| Modified | Unchanged |
| Deleted | All child entities will be null |
Entity Framework Tutorial Basics(22):Disconnected Entities的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(19):Change Tracking
Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...
- Entity Framework Tutorial Basics(12):Model First
Model First development with Entity Framework: In the Model First approach, you create Entities, rel ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- Entity Framework Tutorial Basics(42):Colored Entity
Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- Entity Framework Tutorial Basics(36):Eager Loading
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...
随机推荐
- 细说移动前端Android联调
为什么要联调 A:正在疯狂coding的时候,产品MM过来,焦急的说两周前的一个页面在手机上显示略微错位,但小本上显示正常! B:本着爱折腾的原则,作为大前端,在移动互联网时代的基本技能. 联调的方式 ...
- 2016 ACM-ICPC EC-Final题解
题目链接 A. Number Theory Problem 题意:给你一个数N,求形如2k-1且小于2N的数中有多少能被7整除. 解法:观察二进制位找规律,答案是N/3. #include<bi ...
- mysql笔记1—安装、配置和基础的数据表操作
本篇笔记主要分为两部分: 1,安装完毕之后的简单配置 2,数据的类型.简单的数据表操作命令 一.mysql安装完毕之后 windows和linux环境,除mysql的安装.配置有所不同,其他操作一样, ...
- idea 创建maven工程(入门)
转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ...
- [BZOJ5251][多省联测2018]劈配
bzoj luogu sol 从前往后依次加边,每次对一个人做完劈配后就把当前这个残余网络存下来.这样第二问就可以二分排在第几名然后check一下在对应排名的残余网络上还能不能再增广. 给网络流开结构 ...
- MyEclipse安装jbpm插件
介绍如何在MyEclipse8.6里安装jbpm插件. 工具/原料 MyEclipse8.6 jbpm-4.4.rar 方法/步骤 1 下载jbpm包并解压 下载最新的jbpm包,本文以jbpm4.4 ...
- 蓝桥杯 算法训练 ALGO-117 友好数
算法训练 友好数 时间限制:1.0s 内存限制:256.0MB 问题描述 有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的.例如: 9的约数和有:1+3=4 ...
- HDOJ4768(二分区间)
Flyer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- SPRING中的线程池ThreadPoolTaskExecutor
一.初始化 1,直接调用 ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor(); //线程池所使用的缓冲队列 p ...
- Regexp:正则表达式应用——实例应用
ylbtech-Regexp:正则表达式应用——实例应用 1. 实例应用返回顶部 1. 1.验证用户名和密码:("^[a-zA-Z]\w{5,15}$")正确格式:"[A ...