EF中执行原生sql与使用Local获取本地数据
使用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获取本地数据的更多相关文章
- Django中执行原生SQL语句【新编辑】
参考我的个人博客 这部分迁移到了个人博客中:Django中执行原生SQL语句 这里需要补充一下,还有一个extra方法: ret = models.Student.objects.all().extr ...
- thinkPHP框架中执行原生SQL语句的方法
这篇文章主要介绍了thinkPHP框架中执行原生SQL语句的方法,结合实例形式分析了thinkPHP中执行原生SQL语句的相关操作技巧,并简单分析了query与execute方法的使用区别,需要的朋友 ...
- django系列5.4--ORM中执行原生SQL语句, Python脚本中调用django环境
ORM执行原生sql语句 在模型查询API不够用的情况下,我们还可以使用原始的SQL语句进行查询. Django 提供两种方法使用原始SQL进行查询:一种是使用raw()方法,进行原始SQL查询并返回 ...
- 在ABP中通过EF直接执行原生Sql的解决方案
一般情况下,使用EF中的查询语法和方法语法可以帮助我们完成绝大部分业务,但是也有特殊的情况需要直接执行Sql语句.比如,我们的业务过于复杂繁琐,或是有些业务使用EF操作时比较复杂,但是使用Sql时会很 ...
- 在EF中使用原生SQL,首先要创建上下文对象
using (var db = new Entities()) { //数据操作 } 新增 string sql = "insert into UserInfo values('zhangs ...
- asp.net EF框架执行原生SQL语句
1.执行无参数sql: string sql = "select * from IntegralInfo where convert(nvarchar,getdate(),23)='{0}' ...
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
easyui datagrid 禁止选中行 没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...
- EF中执行sql语句,以及事务
EF to sql string sql = "select T_Task.BSID,T_Task.CloseDate,T_Task.CompleteDate,T_Task.CloseUse ...
- 在EF中执行SQL语句(转载)
在EF中执行SQL语句 你可能要问,我用EF不就为了避免写SQL吗?如果要写SQL我不如直接用ADO.NET得了.话虽然这么说没错,可有些时候使用EF操作数据还是有一些不方便,例如让你根据条件删除 ...
随机推荐
- arcgis server "System.Web.Services.Protocols.SoapException: Error processing server request".
在 Arcgis Server 10中创建第一个程序,运行的时候就报错:System.Web.Services.Protocols.SoapException: Error processing se ...
- Implement Insert and Delete of Tri-nay Tree
问题 Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but wi ...
- PHP扩展开发(2) - VS2013环境搭建
1. 安装VS2013 2. Cygwin安装 3. 下载Windows的PHP源码 4. 修改~/ext/ext_skel_win32.php 加上 $cygwin_path = 'c:\c ...
- 今日分享一点干货。PHP中课程表的实现。
首先贴代码,代码贴完再细说: 前段HTML: <div id="studentRead" class="reading" style="z-in ...
- OOAD基本概念
学习目标: 1.理解与掌握面向对象的概念与方法. 2.使用UML. 3.完成面向对象的分析与设计工作. 4.了解OO的设计原则及一些典型的设计模式 什么是面向对象? 面向对象(Object-Orien ...
- MySQL历史版本下载(官方)
http://downloads.mysql.com/archives/community/ 社区版本(开源免费)
- ubuntu 启用apache2 虚拟机配置
Ubuntu 启用apache2 虚拟机配置 http://jingyan.baidu.com/article/5d6edee20b78e999eadeecf7.html
- Caffe--solver.prototxt配置文件 参数设置及含义
####参数设置################### 1. ####训练样本### 总共:121368个batch_szie:256将所有样本处理完一次(称为一代,即epoch)需要:121368/ ...
- 网站卡死,照惯例运行.bat批量处理文件进行重启不起作用
网站卡死,照惯例运行.bat批量处理文件进行重启不起作用,进入虚拟机控制台进行虚拟机重启仍然不起作用,通过ping分析物理服务器能ping通,各个虚拟机之前也能ping通,但是不能ping通外部,分析 ...
- git新手碰到的各种奇葩问题之一
git 操作错误: <1>.情景描述:当在git commit --amend 更新上一次提交时,而此时提交日志会跳转到别人的日志中.,会出现错误:如下 弥补操作: 1.git fetc ...