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. angularjs中的路由介绍详解 ui-route

    这篇文章主要介绍了Angularjs中UI Router全攻略,涉及到angularjs ui router的基本用法,需要的朋友参考下吧   首先给大家介绍angular-ui-router的基本用 ...

  2. Redis, Memcache, Mysql差别

    在使用Redis过程中,我们发现了不少Redis不同于Memcached.也不同于MySQL的特征. (本文主要讨论Redis未启用VM支持情况) 1. Schema MySQL: 需事先设计 Mem ...

  3. zabbix api调用

    zabbix api调用 api能干什么 Zabbix API allows you to programmatically retrieve and modify the configuration ...

  4. rm 命令简要

    rm   单独使用只能删除文件不能删除文件夹    rm -r 可以删除文件夹和文件 1.rm   test.txt   删除文件 2.rm   -r   test.txt   每次删除的时候都询问是 ...

  5. Swift_1_基本数据类型

    import Foundation println("Hello, World!"); var v1 = 1; var v2 = 2; println(" v1 is \ ...

  6. 安卓TabHost+ViewPager+RadioGroup多功能模板整理

    如今安卓比較流行的布局就是类似新闻client和手机QQ那种的底端可选择,上面的个别页面能够滑动选择. 在測试过程中发现用安卓自带的TabHost去构建.非常难得到自己定义的效果. 因此採用TabHo ...

  7. 13 Memcached 永久数据被踢现象

    一:Memcached 永久数据被踢现象(1)网上有人反映"memcached"数据丢失,明明设为永久不失效,却莫名其妙的丢失了. 其实这要从2个方面来找原因. 即使前面介绍的惰性 ...

  8. Python 内建的filter()函数用于过滤序列。

    例如,在一个list中,删掉偶数,只保留奇数,可以这么写: def is_odd(n): return n % 2 == 1 list(filter(is_odd, [1, 2, 4, 5, 6, 9 ...

  9. android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!

    转载请注明出处:王亟亟的大牛之路 如今二维码已经渗透了我们的生活.各种扫码关注啊.扫码下载的,今天上一个依据输入内容生成二维码的功能. 包结构: 界面截图: 功能:输入网址–>生成图片–> ...

  10. PHP fsockopen模拟POST/GET方法

    原文链接:http://www.nowamagic.net/academy/detail/12220214 fsockopen 除了前面小节的模拟生成 HTTP 连接之外,还能实现很多功能,比如模拟  ...