Entity Framework Tutorial Basics(17):DBSet Class

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:

dbset

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

Method NameReturn TypeDescription
AddAdded entity typeAdds 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 parameterAttaches the given entity to the context in the Unchanged state 

Example: 
dbcontext.Students.Attach(studentEntity);
CreateEntityCreates 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 typeUses 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);
IncludeDBQueryReturns 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>();
RemoveRemoved entityMarks 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);
SqlQueryDBSqlQueryCreates 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.

转载于:https://www.cnblogs.com/purplefox2008/p/5649033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值