DBSet类表示一个实体的集合,用来创建、更新、删除、查询操作,DBSet<TEntity>是DBSet的泛型版本

你可以使用DbContext获取DBSet的引用,例如dbContext.Students

DBSet中的一些重要方法

Method Name Return Type Description
Add Added entity type Adds the given entity to the context the Added state. When the changes are being saved, the entities in the Added states are inserted into the database. After the changes are saved, the object state changes to Unchanged.

Example: 
dbcontext.Students.Add(studentEntity)

AsNoTracking<Entity> DBQuery<Entity> Returns a new query where the entities returned will not be cached in the DbContext. (Inherited from DbQuery.)

Entities returned as AsNoTracking, will not be tracked by DBContext. This will be significant performance boost for read only entities. 

Example: 
var studentList = dbcontext.Students.AsNoTracking<Student>().ToList<Student>();

Attach(Entity) Entity which was passed as parameter Attaches the given entity to the context in the Unchanged state

Example: 
dbcontext.Students.Attach(studentEntity);

Create Entity Creates a new instance of an entity for the type of this set. This instance is not added or attached to the set. The instance returned will be a proxy if the underlying context is configured to create proxies and the entity type meets the requirements for creating a proxy.

Example: 
var newStudentEntity = dbcontext.Students.Create();

Find(int) Entity type Uses the primary key value to attempt to find an entity tracked by the context. If the entity is not in the context then a query will be executed and evaluated against the data in the data source, and null is returned if the entity is not found in the context or in the data source. Note that the Find also returns entities that have been added to the context but have not yet been saved to the database.

Example: 
Student studEntity = dbcontext.Students.Find(1);

Include DBQuery Returns the included non generic LINQ to Entities query against a DbContext. (Inherited from DbQuery)

Example:
var studentList = dbcontext.Students.Include("StudentAddress").ToList<Student>();
var studentList = dbcontext.Students.Include(s => s.StudentAddress).ToList<Student>();

Remove Removed entity Marks the given entity as Deleted. When the changes are saved, the entity is deleted from the database. The entity must exist in the context in some other state before this method is called.

Example:
dbcontext.Students.Remove(studentEntity);

SqlQuery DBSqlQuery Creates a raw SQL query that will return entities in this set. By default, the entities returned are tracked by the context; this can be changed by calling AsNoTracking on theDbSqlQuery<TEntity> returned from this method.

Example:
var studentEntity = dbcontext.Students.SqlQuery("select * from student where studentid = 1").FirstOrDefault<Student>();

EntityFramework 学习 一 DbSet的更多相关文章

  1. entityframework学习笔记--005-给code first一个正确的解释

    在微软官方关于ef7的介绍中强调,ef7将舍弃database first.model first,只保留code first的使用.这引起了很多人的担忧,担忧源自对code first的错误理解.因 ...

  2. entityframework学习笔记--001

    最近想重新好好学习一下entityframework,于是在院子里找到了一篇不错的博客.下面把学习的过程记录下来,方便以后复习. 学习过程参考大神的博客:http://www.cnblogs.com/ ...

  3. EntityFramework 学习资料

    1.EF框架step by step 2.Entity Framework Code First 学习日记 3.[译著]Code First :使用Entity. Framework编程 4.Enti ...

  4. EntityFramework 学习 一 Persistence in Entity Framework

    实体框架的持久化 当用EntityFramework持久化一个对象时,有两种情形:连接的和断开的 1.连接场景:使用同一个context上下文从数据库中查询和持久化实体时,查询和持久化实体期间,con ...

  5. EntityFramework 学习 一 Querying with EDM 从EDM查询

    前面我们已经创建EDM.DbContext和实体类,接下来我们学习不同的查询实体方法,转变为数据库的SQL查询 Entity Framework支持3种查询方式:1)LINQ to Entities ...

  6. entityframework学习笔记--009-使用原生sql语句操作数据

    1 使用原生SQL语句更新--Database.ExecuteSqlCommand 假设你有一张如图9-1所示的Payment数据库表. 图9-1 1.1 实体类型: public class Pay ...

  7. entityframework学习笔记--008-实体数据建模基础之继承关系映射TPH

    Table per Hierarchy Inheritance 建模 1.让我们假设你有如图8-1中的表,Employee表包含hourly employees 和salaried employees ...

  8. entityframework学习笔记--007-实体数据建模基础之继承关系映射TPT

    Table per Type Inheritance (TPT)建模 1.假设你有两张表与一张公共的表密切相关,如图7-1所示,Businiss表与eCommerce表.Retail表有1:0...1 ...

  9. entityframework学习笔记--006-表拆分与实体拆分

    1.1 拆分实体到多张表 假设你有如下表,如图6-1.Product表用于存储商品的字符类信息,ProductWebInfo用于存储商品的图片,两张表通过SKU关联.现在你想把两张表的信息整合到一个实 ...

随机推荐

  1. Hibernate学习之属性级别注解

    © 版权声明:本文为博主原创文章,转载请注明出处 属性级别注解 添加方式 1. 写在属性字段上面 2. 写在属性getter方法上面 @Id:必须,定义了映射到数据库表的主键属性,一个实体可以有一个或 ...

  2. objective-C中的扩展方法与partial class

     在c#中要扩展一个现有类非常easy,比方这样: ? 1 2 3 4 5 6 7 public static class Utils {     public static void PrintTo ...

  3. HTML5 2D平台游戏开发#10Wall Jump

    这个术语不知道怎么翻译比较贴切,但并不妨碍对字面意思的理解,大概就是飞檐走壁.比如: 这是游戏<忍者龙剑传>中的场景,玩家可以通过操纵角色在墙面上移动并跳跃. 首先需要实现角色抓墙这一动作 ...

  4. tftp服务配置

    tft概念: TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文 ...

  5. 【Python+selenium Wendriver API】之操作警告和弹出框

    参考文章: <Python+Selenium笔记(九):操作警告和弹出框>

  6. 【windows7 + Appium】之Appium安装以及其他工具安装配置

    首先感谢虫师总结的教程:<appium新手入门>.以及:<appium新手入门(2)—— 安装 Android SDK> 目录: 安装Appium&安装node.js ...

  7. Linux5_环境变量

    1.总结背景 在linux系统下,下载并安装了应用程序,很有可能在键入它的名称时出现“command not found”的提示内容. 每次都到安装目标文件夹内,找到可执行文件来进行操作就太繁琐了.这 ...

  8. 软件测试人员需要精通的开发语言(1)--- VBScript

    软件测试不局限于点点点的纯黑盒测试,提升自身的代码能力也是事关重要的.软件测试的发展,越来越多的公司对于测试人员的要求也日益提高,测试人员必备开发能力的优势也凸显出来.简单的介绍下部分开发语言的学习及 ...

  9. iOS框架你了解多少?

    1.iOS 系统可以分为以下四层,每个框架对应IOS系统里的一层,每层建立在它下面层的上面.应该尽量使用上层的框架来代替下面的框架.更高层次的框架是对底层框架基于对象的抽象.以下列出几个iOS开发的常 ...

  10. PHP计算多少秒/分/时/天/周/月/年之前 : timeago

    function timeago( $ptime ) { $etime = time() - $ptime; if ($etime < 59) return '刚刚'; $interval = ...