Entity Framework Tutorial Basics(26):Add Entity Graph
Add Entity Graph using DbContext:
Adding entity graph with all new entities is a simple task. We can use DbSet.Add() method to attach a whole entity graph to the context and set all the entity's states to Added as we have seen in theprevious section.
The following example adds a student entity graph in disconnected mode, using the DbSet.Add() method which marks the added state to all the entities:
//Create student in disconnected mode
Student newStudent = new Student() { StudentName = "New Single Student" }; //Assign new standard to student entity
newStudent.Standard = new Standard() { StandardName = "New Standard" }; //add new course with new teacher into student.courses
newStudent.Courses.Add(new Course() { CourseName = "New Course for single student", Teacher = new Teacher() { TeacherName = "New Teacher" } }); using (var context = new SchoolDBEntities())
{
context.Students.Add(newStudent);
context.SaveChanges(); Console.WriteLine("New Student Entity has been added with new StudentId= " + newStudent.StudentID.ToString());
Console.WriteLine("New Standard Entity has been added with new StandardId= " + newStudent.StandardId.ToString());
Console.WriteLine("New Course Entity has been added with new CourseId= " + newStudent.Courses.ElementAt().CourseId.ToString());
Console.WriteLine("New Teacher Entity has been added with new TeacherId= " + newStudent.Courses.ElementAt().TeacherId.ToString());
}
New Student Entity has been added with new StudentId= 14
New Standard Entity has been added with new StandardId= 6
New Course Entity has been added with new CourseId= 7
New Teacher Entity has been added with new TeacherId= 9
This executes the following DDL statements to the database:
exec sp_executesql N'INSERT [dbo].[Standard]([StandardName], [Description])
VALUES (@0, NULL)
SELECT [StandardId]
FROM [dbo].[Standard]
WHERE @@ROWCOUNT > 0 AND [StandardId] = scope_identity()',N'@0 varchar(50)',@0='New Standard'
go exec sp_executesql N'INSERT [dbo].[Student]([StudentName], [StandardId])
VALUES (@0, @1)
SELECT [StudentID]
FROM [dbo].[Student]
WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity()',N'@0 varchar(50),@1 int',@0='New Single Student',@1=61
go exec sp_executesql N'INSERT [dbo].[Teacher]([TeacherName], [StandardId])
VALUES (@0, NULL)
SELECT [TeacherId]
FROM [dbo].[Teacher]
WHERE @@ROWCOUNT > 0 AND [TeacherId] = scope_identity()',N'@0 varchar(50)',@0='New Teacher'
go exec sp_executesql N'INSERT [dbo].[Course]([CourseName], [Location], [TeacherId])
VALUES (@0, NULL, @1)
SELECT [CourseId]
FROM [dbo].[Course]
WHERE @@ROWCOUNT > 0 AND [CourseId] = scope_identity()',N'@0 varchar(50),@1 int',@0='New Course for single student',@1=93
go exec sp_executesql N'INSERT [dbo].[StudentCourse]([StudentId], [CourseId])
VALUES (@0, @1)
',N'@0 int,@1 int',@0=43,@1=72
go
Thus, there is no difference when you add a single entity or entity graph in disconnected or connected scenario. Make sure that all the entities are new.
Learn how to update entity graph in disconnected scenario in the next section.
Entity Framework Tutorial Basics(26):Add Entity Graph的更多相关文章
- 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(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(27):Update Entity Graph
Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...
- Entity Framework Tutorial Basics(23):Add Single Entity
Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...
- Entity Framework Tutorial Basics(40):Validate Entity
Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(32):Enum Support
Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...
- Entity Framework Tutorial Basics(31):Migration from EF 4.X
Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...
随机推荐
- redis 双写一致性问题
首先,缓存由于其高并发和高性能的特性,已经在项目中被广泛使用.在读取缓存方面,大家没啥疑问,都是按照下图的流程来进行业务操作. 但是在更新缓存方面,对于更新完数据库,是更新缓存呢,还是删除缓存.又或者 ...
- js1
document.write('<h1>Hello World</h1>'); //写入网页 alert('Hello World'); console.log('Hello ...
- BZOJ1116:[POI2008]CLO
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- 三、Jmeter--HTTP请求默认值(HTTP Request Defaults)和访问地址参数化
一.HTTP请求默认值(HTTP Request Defaults) 1. 在本地搭建了一个wordpress开源论坛,那么我每次访问论坛的地址(服务器名称或IP)是不变的,端口也是不变的,协议也是不 ...
- Hibernate检索方式(转载)
我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位. 那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为H ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- ArcGis教程
91卫图助手帮助中心-如何导出ArcGIS Server瓦片格式并进行发布 http://help.91weitu.com/rhdcarcgis%20serverwpgsbjxfb.html ArcG ...
- 接口方式[推荐]/动态SQL语句
MVC目录结构: Src -- com.shxt.servlet[控制层] --com.shxt.service[业务逻辑层] --com.shxt.model[实体Bean,用来承载数据] --co ...
- eclipse项目中将普通文件夹转化成资源文件夹
1.点选该文件夹 2.右键属性Properties 3.选择属性:Build Path 4.点选属性:Use as Source Folder ......等待变成资源文件夹 参考更详细的: ecl ...
- 任意输入3个数,判断能否组成三角形(python)
任意输入3个数,判断能否组成三角形. 三角形:两边之和大于第三边 直角三角形:勾股定理 代码如下: # 输入合法性检查,必须输入正数,不支持科学计数法'''try: <语句>except ...