A DbSet represents the collection of all entities in the context. Is used for Add, update, delete and read operations. You can also use generic type of DBSet (DbSet<TEntity>) when the entity type is unknown at build time.  DbSet objects are created from a DbContext using the DbContext.Set method 

The DbContext class includes DbSet as shown below:a

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

Method Name

Return Type

Description

Add

Added entity type

Add the given entity to the context the Added state. When the Save changes are completed, the entities in the Added states are inserted into the database. After the Save changes complete, the object state changes to Unchanged.

Example: 
ArticleDBEntities db = new ArticleDBEntities();

ArticlePost entity = new ArticlePost();

entity.Title = " What is DBSET In entity Farmework";

 db.ArticlePosts.Add(entity);           

AsNoTracking<Entity>

DBQuery<Entity>

It Returns a new query where the entities returned will not be cached in the DbContext. (Inherited from DbQuery.)


Example: 
ArticleDBEntities db = new ArticleDBEntities();

 var ArticletList = db.ArticlePosts.AsNoTracking<ArticlePost>().ToList<ArticlePost>();

Attach(Entity)

Entity which was passed as parameter

Attaches the given entity to the context in the Unchanged state 

Example: 
ArticleDBEntities db = new ArticleDBEntities();

 ArticlePost entity = new ArticlePost();

 entity.Title = " What is DBSET In entity Farmework";

db.ArticlePosts.Attach(entity);

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: 
ArticleDBEntities db = new ArticleDBEntities();

var data= db.ArticlePosts.Find(1);

Remove

Removed entity

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

Example:
  ArticleDBEntities db = new ArticleDBEntities();

  var ArticlePostEntity = db.ArticlePosts.Find(1);

  db.ArticlePosts.Remove(ArticlePostEntity);

SqlQuery

DBSqlQuery

Creates an inline 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:
 ArticleDBEntities db = new ArticleDBEntities();

var data = db.ArticlePosts.SqlQuery("select * from ArticlePost where ID = 1").FirstOrDefault<ArticlePost>();

 

Thanks.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like