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. 在ios7中使用zxing

    ZXing(Github镜像地址)是一个开源的条码生成和扫描库(开源协议为Apache2.0).它不但支持众多的条码格式,而且有各种语言的实现版本,它支持的语言包括:Java, C++, C#, Ob ...

  2. uva11489 - Integer Game(考思维,找规律)

    从S开始时只能是两种情况: 1.现在总和已经是3的倍数了,那么因为每人每次只能拿走一个数,要保持拿走之后依然是3的倍数,那么就只能拿3,6,9这类数,用num统计一下,看看num奇偶性就知道谁最后拿了 ...

  3. [BZOJ5248][多省联测2018]双木棋chess

    bzoj luogu sol 首先,要保证一个格子的左边和上方都放满了棋子,就需要这个点的左上方那个矩形都放满了棋子. 这样放棋子状态就会是一个自左下至右上的轮廓线. 状态数?\(C_{20}^{10 ...

  4. Java String Split Method

    Java String.split() method 有如下几种特殊情况: 1. 分隔符出现在首尾 public static void main(String args[]) { String St ...

  5. java 简单实现socket

    server端 package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  6. 学习动态性能表(9)--v$filestat

    学习动态性能表 第九篇--V$FILESTAT  2007.6.5 本视图记录各文件物理I/O信息.如果瓶颈与I/O相关,可用于分析发生的活动I/O事件.V$FILESTAT显示出数据库I/O的下列信 ...

  7. springCloud组件启动时,提示内部tomcat无法加载

    忘记拷贝当时的异常信息了. 本地启动模块,做测试 大概就是emmble tomcat就是表示内部tomcat无法启动. 出现这样的异常是由于配置中心得代码已经修改,但是没有从git上down下来,然后 ...

  8. MongoDB数据库的备份和恢复

    MongoDB数据库备份方式: 1.整库备份 2.单表备份 1.整库备份 备份整个数据库: mongodump -h 127.0.0.1:27000 -d park --authenticationD ...

  9. ORA-28547:connection to server failed, probable Oracle Net admin error错误,解决方法

    当用navicat连接oralce数据库时报ORA-28547错误时,直接懵逼了,上网查了资料说是navicat自带的oci.dll文件的版本和服务器端的oralce数据库的版本不一致造成的. 修改O ...

  10. Boost.Asio基本原理(CSDN也有Markdown了,好开森)

    Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将深入研究比同步编程更复杂.更有乐趣的异步编程. 网络API 这一部分包含了当使用Boost.Asio编写 ...