EntityFramework 学习 一 Disconnected Entities
如何把断开的实体添加到新的context上下文中
1.首先,我们需要把实体附加到新的context上下文实例中。
2.其次,手动的给实体设置适当的实体状态,因为新的context上下文不知道断开的实体上有什么操作。
DbSet.Add():
把实体添加到context上下文,并自动设置实体的属性为added状态
//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);
}
DbSet.Attach():
把实体添加到context中,并设置为Unchanged状态
//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);
}
通过DBContext.Entry()方法获取指定实体的状态,
DbContext.Entry(disconnectedEntity).state = EntityState.Added/Modified/Deleted/Unchanged
Parent Entity State | Entity State of child entities |
---|---|
Added | Added |
Modified | Unchanged |
Deleted | All child entities will be null |
EntityFramework 学习 一 Disconnected Entities的更多相关文章
- Entity Framework Tutorial Basics(22):Disconnected Entities
Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...
- EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario
Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...
- EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario
using System; using System.Collections.Generic; public partial class Student { public Student() { th ...
- EntityFramework 学习 一 Add New Entity using DBContext in Disconnected Scenario
using System; using System.Collections.Generic; public partial class Student { public Student() { th ...
- EntityFramework 学习 一 Querying with EDM 从EDM查询
前面我们已经创建EDM.DbContext和实体类,接下来我们学习不同的查询实体方法,转变为数据库的SQL查询 Entity Framework支持3种查询方式:1)LINQ to Entities ...
- entityframework学习笔记--001
最近想重新好好学习一下entityframework,于是在院子里找到了一篇不错的博客.下面把学习的过程记录下来,方便以后复习. 学习过程参考大神的博客:http://www.cnblogs.com/ ...
- EntityFramework学习
本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...
- EntityFramework 学习资料
1.EF框架step by step 2.Entity Framework Code First 学习日记 3.[译著]Code First :使用Entity. Framework编程 4.Enti ...
- EntityFramework 学习 一 Change Tracking in Entity Framework
EntityFramework自动跟踪上下文中已经加载的实体,DbChangeTracker类给你关于当前实体的所有跟踪信息 注意,每个实体都要有EntityKey(主键)的属性,EntityFram ...
随机推荐
- php单元测试入门教程phpunit详解
本文档提供了一些phpunit官方教程没有提到的信息,帮助初学者快速了解php单元测试,在phpunit官网提供了详细的中文教程,可选多种格式下载 phpunit官网地址:https://phpuni ...
- AngularJS路由 $state服务、路由事件、获取路由参数
1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...
- [LeetCode]Palindrome Number 推断二进制和十进制是否为回文
class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...
- 启动avd Android模拟器缓慢 HAXM自动安装失败
问题1.更新Android sdk镜像,腾讯镜像地址 android-mirror.bugly.qq.com 使用方法如图 问题2.自动更新HAXM失败解决方法 手动下载地址 http://softw ...
- "下列引导或系统启动驱动程序无法加载: cdrom"的解决方案
1.进入注册表(开始->运行->regedit) 2.展开HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\cdrom 3.把Sta ...
- 机器学习7—AdaBoost学习笔记
Adaboost算法原理分析和实例+代码(简明易懂)(转载) [尊重原创,转载请注明出处] http://blog.csdn.net/guyuealian/article/details/709953 ...
- Mongodb搭建
1.配置yum源,创建/etc/yum.repos.d/mongodb-org-3.2.repo文件,添加如下文件内容: [mongodb-org-3.2] name=MongoDB Reposito ...
- 【转】【Python学习】之哪些 Python 库让你相见恨晚?
感谢作者:赖明星 文章链接地址:<哪些 Python 库让你相见恨晚?>
- Cannot merge new index 65781 into a non-jumbo instruction! 问题解决(网上摘抄)
我的报了这个错 Error:Execution failed for task ':app:transformClassesWithDexForDebug'.> com.android.buil ...
- mysql-5.1.73多实例安装启动
一.源码包下载:http://download.softagency.net/MySQL/Downloads/MySQL-5.1/ 二.编译安装 groupadd mysql useradd -r - ...