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.

Entity Framework Tutorial Basics(17):DBSet Class的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  2. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  3. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  4. Entity Framework Tutorial Basics(42):Colored Entity

    Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...

  5. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  6. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  7. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  8. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  9. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

随机推荐

  1. 14_ Component 游戏开发组件模式

    # component 不同作用的程序需要保持互相隔离 我们不想ai 物理 渲染 声音 等等功能 耦合在一起,像下面这样 ``` //bad if (collidingWithFloor() & ...

  2. python list的extend和append方法

    append: Appends object at the end. x = [1, 2, 3] x.append([4, 5]) print (x) gives you: [1, 2, 3, [4, ...

  3. 非root用户 如何将cscope安装到指定目录,vim74安装

    随着Linux的普及,使用Linux进行软件开发的人也越来越多.而大多数公司都采用这种方式:提供一台高性能的中央服务器做为开发编译服务器,每个人登录这台服务器进行开发编译.在这种情况下,用户通常没有r ...

  4. Apache POI实现excel导出

    链接:http://poi.apache.org/ Excel数据导出步骤: 使用poi 完成账户数据的导出功能 导入poi  jar包并添加到classpath中 1.查询数据 2.定义导出头 St ...

  5. 洛谷P2706 巧克力

    题目背景 王7的生日到了,他的弟弟准备送他巧克力. 题目描述 有一个被分成n*m格的巧克力盒,在(i,j)的位置上有a[i,j]块巧克力.就在送出它的前一天晚上,有老鼠夜袭巧克力盒,某些位置上被洗劫并 ...

  6. 什么是 PCB 的压适孔

    引用 AMOBBS 1 再举一个高成本控制的例子:有类PCB产品对孔径要求极度严格,这类孔叫压适孔,这类孔的作用类似于显卡内存条的插座,能刚刚好被元件插上,而且元件不会掉,PTH的压适孔公差要求为-0 ...

  7. AfxExtractSubString 函数的相关问题

    AfxExtractSubString函数的用法 注:本文系rainy8758原创,转载请注明出处:http://blog.hjenglish.com/rainy8758/articles/10109 ...

  8. centos7 安装 mysql-python时 报错 EnvironmentError: mysql_config not found

    pip install mysql-python 然后报错 EnvironmentError: mysql_config not found 网上搜解决方法,需要安装   mysql-devel 然后 ...

  9. SQL SERVER存储过程的几种示例

    1.常用系统存储过程及使用语法:exec sp_databases; --查看数据库exec sp_tables; --查看表exec sp_columns student;--查看列exec sp_ ...

  10. FPGA的年龄

    FPGA的年龄 1984年,Xilinx公司发布了第一个FPGA(但直到1985年这些器件才真正发货).尽管这些器件比当时那些简单的可编程逻辑器件(PLD)复杂的多,但大多数数字设计工程师却仅仅用这些 ...