DBSet Class

DBSet class represents an entity set that is used for create, read, update, and delete operations. A generic version of DBSet (DbSet<TEntity>) can be used when the type of entity is not known at build time.

You can get the reference of DBSet by using DBContext, eg. dbcontext.Students, dbcontext.Standards, or any other entity set. DbContext class includes DbSet as shown below:

Some of the important methods of DBSet class are shown in the table below::

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>();

Visit MSND for more information on DBSet class.

最新文章

  1. 解决 uuid.h找不到的问题
  2. Scala入门之控制结构
  3. 营业额统计(bzoj1588)
  4. Python3操作MySQL,查询数据并保存到文件中
  5. 导入maven工程并配置maven环境
  6. LR Socket 测试demo
  7. 针对主流浏览器的CSS-HACK写法及IE常用条件注释
  8. httpsClient实例
  9. native2ascii 在 Mac终端的转码
  10. 阻碍android程序员发展的几个原因
  11. discuz方法赏析
  12. Shell脚本编程具体解释
  13. MySQL5.7 安装过程中出现 attempting to start service 过不去
  14. 随便讲讲XSS攻击
  15. 第一百零二节,JavaScript函数
  16. OpenGL-----深度测试,剪裁测试、Alpha测试和模板测试
  17. Nmap脚本文件分析(AMQP协议为例)
  18. pycharm opencv4.0安装使用
  19. 7.mysql-安装和卸载.md
  20. MyEclipse的破解和汉化方法

热门文章

  1. Windows之IOCP
  2. @Override重写
  3. Swap Adjacent Elements
  4. [HDU5324]Boring Class
  5. LeetCode Number of Longest Increasing Subsequence
  6. Proxmox qm命令应用实例
  7. sleep(0)作用
  8. JSF拦截ajax请求并传递参数方法
  9. JPA,EclipseLink 缓存机制学习——树节点搜索问题引发的思考
  10. 【转】Jquery修改image的src属性,图片不加载问题