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.

最新文章

  1. nginx 和 IIS 实现负载均衡
  2. JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
  3. google closure--继承模块一:goog.inherits()
  4. iOS - C 基本语法
  5. 苹果HomeKit如何牵动全国智能硬件格局
  6. Excel下用SQL语句实现AVEDEV函数功能
  7. Cocos2d-x3.0游戏实例《不要救我》第十篇(结束)——使用Json配置数据类型的怪物
  8. 用Maven打包成EAR远程部署JBoss(二)——部署到远程JBoss
  9. C++ 关联容器之map插入相同键元素与查找元素操作
  10. for语句,你真正搞懂了吗?
  11. linux cenots7安装mysql
  12. [爬虫]Fiddler证书错误
  13. ios开发中字符串的常用功能总结
  14. 线程 学习教程(一): Java中终止(销毁)线程的方法
  15. A1086. Tree Traversals Again
  16. LeetCode122.买卖股票的最佳时机II
  17. [UE4]Text Block文字字体偏移
  18. Mysql 多字段去重
  19. Install Hyper-V on Windows 10
  20. Jenkins+Github配置【转】

热门文章

  1. Ajax做无刷新分页
  2. Scala极速入门
  3. Office 2007在安装过程中出错
  4. 如何使用FlashFXP上传网站程序?
  5. tensorflow 学习笔记-1
  6. ThinkPHP中的find和select的区别
  7. POJ2411Mondriaan's Dream(DP+状态压缩 or 插头DP)
  8. DCOS安装
  9. 14.Selenium+Python使用火狐浏览器问题解决
  10. Ubuntu 16.04安装Elasticsearch,Logstash和Kibana(ELK)Filebeat