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());
}
Output:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Entity Framework Tutorial Basics(27):Update Entity Graph

    Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...

  4. 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 ...

  5. 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 ...

  6. Entity Framework Tutorial Basics(40):Validate Entity

    Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...

  7. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. PHP学习之数组Array操作和键值对操作函数(一)

    PHP 中的数组实际上是一个有序映射.映射是一种把 values关联到 keys 的类型.此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合, ...

  2. java实现sendemail

    <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</arti ...

  3. jsp有哪些动作?作用分别是什么?

    jsp共有6种基本动作: 1.jsp:include,在页面被请求的时候引入一个文件 2.jsp:useBean,寻找或者实例化一个JavaBean 3.jsp:setProperty,设置JavaB ...

  4. Linux终端录屏与播放 script 命令

    本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4107700.html ,转载请保留此声明. 有时候可能想要记录在终端的所有操作包括输出等,将来作为视频播 ...

  5. BZOJ3141:[HNOI2013]旅行

    浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  6. [转载]rmmod: can't change directory to '/lib/modules': No such file or directory

    转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...

  7. (转)基于PHP——简单的WSDL的创建(WSDL篇)

    本文转载自:http://blog.csdn.net/rrr4578/article/details/24451943 1.建立WSDL文件     建立WSDL的工具很多,eclipse.zends ...

  8. HTML 和 CSS

    HTML html是英文hyper text mark-up language(超文本标记语言)的缩写,它是一种制作万维网页面标准语言.   内容摘要   Doctype 告诉浏览器使用什么样的htm ...

  9. HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. Pythonb编码规范

    本编码规范是对知道创宇研发技能表中提供的PythonCodingRule.pdf文档进行凝练和总结出来的结果,感谢知道创宇的Geek精神与分享精神 此规范较为严格,严格规定了编码格式和命名规则,仅适于 ...