DBSet in Entity Framework

In the last tutorial, we looked at the DbContext class in detail. In this tutorial let us look at the DbSet and the Methods provided by it to work with the entities

DBSet

A DbSet represents an entity set. An entity set is defined as a set of entities of the same entity type. From the perspective of  the database, it usually represents the table. Each Entity type must expose the DbSet Property to be able to participate in the CRUD Operations. DBSet Provides methods like Add, Attach, remove etc on the Entity Types.

The DBContext class exposes the DBSet Property for each entity in the model. DbSet Implements the IQueryable Interface . This allows us to query the database using the LINQ query. DbSet inherits from DbQuery and exposes the query capabilities

Using DbSet

Methods Exposed by DbSet

Add(TEntity)

This method Adds the given entity to the context in the Added State. These entities is inserted into the database table when the saveChanges on the context is called.

AddRange

This method adds the collection of entities to the context. All the entities in the collection are marked as Added State. All the entities are inserted into the database table when saved by the context (by Calling SaveChanges method)
Note that entities that are already in the context in some other state will have their state set to Added

Attach

Attach method attaches the given entity to the context. State of the attached will be marked as Unchanged. It means calling savechanges on the context will have no effect on the database

AsNoTracking()

It is an extension method where the returned entities are not be tracked by the context. This means that if you want to save the entities you need to attach them to the context and set the state correctly.

Cast<TEntity>()

Returns the equivalent generic DbSet<TEntity> object.

Create()

This method creates and returns a new instance of an entity. This entity is not added or attached to set.

Find

Finds and returns an entity with the given primary key values. Context always looks for the entity in the context first and returns it if found. If it is not found, then a request is sent to the database. The entity if not found then the null is returned

Include(String)

Specifies the related objects to include in the query results.

Remove

The method used to delete the entity from the database. The entity is not deleted immediately. They are marked as deleted. The entity is Deleted from the database when SaveChanges is called.

RemoveRange

The method used to delete the collection of entities. It does so by changing the state of entities to the deleted state. When the SaveChanges method is invoked, Context will delete these entities from the database

SQLQuery

This method uses the raw SQL query to return the entities. It has two parameters. The First Parameter is the SQL query string. The second is the parameters to apply to the SQL query.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top