使用DbSet的Local属性可以访问当前context中被追踪且没有被标记为删除的实体(内存中的数据)

 using (var context = new BloggingContext())
{
// Load all blogs from the database into the context
context.Blogs.Load(); // Add a new blog to the context
context.Blogs.Add(new Blog { Name = "My New Blog" }); // Mark one of the existing blogs as Deleted
context.Blogs.Remove(context.Blogs.Find()); // Loop over the blogs in the context.
Console.WriteLine("In Local: ");
foreach (var blog in context.Blogs.Local)
{
Console.WriteLine(
"Found {0}: {1} with state {2}",
blog.BlogId,
blog.Name,
context.Entry(blog).State);
} // Perform a query against the database.
Console.WriteLine("\nIn DbSet query: ");
foreach (var blog in context.Blogs)
{
Console.WriteLine(
"Found {0}: {1} with state {2}",
blog.BlogId,
blog.Name,
context.Entry(blog).State);
}
}

获取追踪对象的详细信息

 using (var context = new BloggingContext())
{
// Load some entities into the context
context.Blogs.Load();
context.Authors.Load();
context.Readers.Load(); // Make some changes
context.Blogs.Find().Title = "The New ADO.NET Blog";
context.Blogs.Remove(context.Blogs.Find());
context.Authors.Add(new Author { Name = "Jane Doe" });
context.Readers.Find().Username = "johndoe1987"; // Look at the state of all entities in the context
Console.WriteLine("All tracked entities: ");
foreach (var entry in context.ChangeTracker.Entries())
{
Console.WriteLine(
"Found entity of type {0} with state {1}",
ObjectContext.GetObjectType(entry.Entity.GetType()).Name,
entry.State);
} // Find modified entities of any type
Console.WriteLine("\nAll modified entities: ");
foreach (var entry in context.ChangeTracker.Entries()
.Where(e => e.State == EntityState.Modified))
{
Console.WriteLine(
"Found entity of type {0} with state {1}",
ObjectContext.GetObjectType(entry.Entity.GetType()).Name,
entry.State);
} // Get some information about just the tracked blogs
Console.WriteLine("\nTracked blogs: ");
foreach (var entry in context.ChangeTracker.Entries<Blog>())
{
Console.WriteLine(
"Found Blog {0}: {1} with original Name {2}",
entry.Entity.BlogId,
entry.Entity.Name,
entry.Property(p => p.Name).OriginalValue);
} // Find all people (author or reader)
Console.WriteLine("\nPeople: ");
//返回所有实现IPerson接口的类的Name属性值
foreach (var entry in context.ChangeTracker.Entries<IPerson>())
{
Console.WriteLine("Found Person {0}", entry.Entity.Name);
}
} public class Author : IPerson
{
public int AuthorId { get; set; }
public string Name { get; set; }
public string Biography { get; set; }
} public class Reader : IPerson
{
public int ReaderId { get; set; }
public string Name { get; set; }
public string Username { get; set; }
} public interface IPerson
{
string Name { get; }
}

EF中执行原生的sql语句

1.在指定实体上进行查询

using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
}

2.执行存储过程

using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("dbo.GetBlogs").ToList();
} //带参数的存储过程
using (var context = new BloggingContext())
{
var blogId = ; var blogs = context.Blogs.SqlQuery("dbo.GetBlogById @p0", blogId).Single();
}

3.执行sql命令

using (var context = new BloggingContext())
{
var blogNames = context.Database.SqlQuery<string>(
"SELECT Name FROM dbo.Blogs").ToList();
} using (var context = new BloggingContext())
{
context.Database.SqlCommand(
"UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1");
}

EF中执行原生sql与使用Local获取本地数据的更多相关文章

  1. Django中执行原生SQL语句【新编辑】

    参考我的个人博客 这部分迁移到了个人博客中:Django中执行原生SQL语句 这里需要补充一下,还有一个extra方法: ret = models.Student.objects.all().extr ...

  2. thinkPHP框架中执行原生SQL语句的方法

    这篇文章主要介绍了thinkPHP框架中执行原生SQL语句的方法,结合实例形式分析了thinkPHP中执行原生SQL语句的相关操作技巧,并简单分析了query与execute方法的使用区别,需要的朋友 ...

  3. django系列5.4--ORM中执行原生SQL语句, Python脚本中调用django环境

    ORM执行原生sql语句 在模型查询API不够用的情况下,我们还可以使用原始的SQL语句进行查询. Django 提供两种方法使用原始SQL进行查询:一种是使用raw()方法,进行原始SQL查询并返回 ...

  4. 在ABP中通过EF直接执行原生Sql的解决方案

    一般情况下,使用EF中的查询语法和方法语法可以帮助我们完成绝大部分业务,但是也有特殊的情况需要直接执行Sql语句.比如,我们的业务过于复杂繁琐,或是有些业务使用EF操作时比较复杂,但是使用Sql时会很 ...

  5. 在EF中使用原生SQL,首先要创建上下文对象

    using (var db = new Entities()) { //数据操作 } 新增 string sql = "insert into UserInfo values('zhangs ...

  6. asp.net EF框架执行原生SQL语句

    1.执行无参数sql: string sql = "select * from IntegralInfo where convert(nvarchar,getdate(),23)='{0}' ...

  7. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  8. EF中执行sql语句,以及事务

    EF to sql string sql = "select T_Task.BSID,T_Task.CloseDate,T_Task.CompleteDate,T_Task.CloseUse ...

  9. 在EF中执行SQL语句(转载)

    在EF中执行SQL语句   你可能要问,我用EF不就为了避免写SQL吗?如果要写SQL我不如直接用ADO.NET得了.话虽然这么说没错,可有些时候使用EF操作数据还是有一些不方便,例如让你根据条件删除 ...

随机推荐

  1. web前端之文件上传

    前言 最近太忙一直没时间认真的写博客(哈哈哈),最近pm提一个需求,移动端需要一个上传图片的功能,允许多选.删除.预览.点击查看大图并可以滑动.虽然听起来很多,但是这个功能在web上实现过啊,使用we ...

  2. 浅述Oracle分布式事务概念

    着系统的复杂性不断增加,我们所面对的分布式系统渐渐增加.分布式文件系统.分布式消息队列系统等等层出不穷,在一些行业特别是互联网行业应用广泛.分布式数据库也是目前使用比较常用的分布式系统之一. 简单来说 ...

  3. SpringMVC学习简单HelloWorld实例

    首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubuntu12.04(不同操作系统对本系列项目没有影响): 开发工具:Eclipse For JavaEE: 数据库:My ...

  4. 概率dp小结

    好久之前学过,记得是一次亚洲区的前几天看了看概率dp,然后亚洲区就出了一道概率dp,当时虽然做上了,但是感觉有很多地方没懂,今天起早温习了一下,觉得很多地方茅塞顿开,果然学习的话早上效果最好了. 首先 ...

  5. Say bye to CMake and Makefile

    用了几年的CMake,最近想试着琢磨如何将C++应用的动态链接全部改成静态链接,发现还需要研究CMake的用法,进入CMake的文档, http://www.cmake.org/cmake/help/ ...

  6. 编译protobuf-2.5.0中的错误处理

    最近在编译protobuf-2.5.0源码的时候发现的错误已经应对方法 1. 在源码目录执行 ./configure 命令的时候,发生如下错误 error: C++ preprocessor &quo ...

  7. win8下nodejs安装配置记录

    1:打开nodejs官网http://nodejs.org/ 下载安装版. 2:安装完成后,打开cmd输入node -v 查看是否安装成功: 3:安装express,通过全局安装方式进行安装: 安装完 ...

  8. 功率和dB的关系

    功率和dB的关系应该如下: 1.dB的引入是为了把乘除关系变换为加减,便于工程中的运算. 2.[dB] = 10lg(输出功率W/输入功率W).如:输入功率为1W而输出功率为1000W,则系统的增益为 ...

  9. FastMM内存泄露

    转自:http://www.2ccc.com/article.asp?articleid=4879FastMM是非常优秀的内存管理器,但是从FastMM4Options.inc中找到合适自已程序的选项 ...

  10. UNIX网络编程--ioctl操作(十七)

    一.概述 在本书中有两个地方都对这个函数进行了介绍,其实还有很多地方需要这个函数.ioclt函数传统上一直作为纳西而不适合归入其他精细定义类别的特性的系统接口.网络程序(特别是服务器程序)经常在程序启 ...