DbSet in Entity Framework Core

The DbSet represents the collection of all entities in the context. Every model must expose the DbSet property to become part of the Context and managed by it. We use the DbSet to query, insert, update & Delete entities. In this tutorial, we will look into DbSet and its methods in detail.

Modeling the Database

In EF Core we create POCO Classes to represent our database. Each table in the database, will gets its own class. For Example, the Product Table in the database can be represented using the following class. Here id & name represents the columns in the table.

It is a simple POCO class. The EF Core knows nothing about the class. It will not be included in the model and will not be mapped to the database.

DbSet

To Include the above class in the model, we must define a DbSet property of the class. The DbSet property must be included in the Context class.

Typical Context class is as shown below. This is the Context class we used in the Entity Framework Core Console Application tutorial.

We need to create DbSet Property for each & every class, which we want to be part of the model.

EF Core scans all the types, which have a DbSet property and includes them in the model. It also looks for the public properties & types and uses it to determine the columns & types. We can also provide extra modeling information using conventions, data annotation attributes, & Fluent API.

The DBSet Provides methods like Add, Attach, remove, etc on the Entity Types. The Context class maps these operations into a SQL query and runs it against the database using the Database Providers.

DbSet Implements the IQueryable & IEnumerable interfaces This allows us to query the database using the LINQ query.

Using DbSet

The following are few of the examples on how to use DbSet.

Adding Single Record

The Add method adds the given entity to the context in the Added state. The EF Core generates the insert SQL query and updates the database when we call the SaveChanges method.

Syntax

Example

Read more from Add Single Record in EF Core

Adding Multiple Records

This AddRange 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

Syntax

Example

Read more from Add Multiple Records in EF Core

Attaching Records

The Attach method attaches the given entity to the context. The records are marked as Unchanged. It means calling savechanges on the context will have no effect on the database

Finding an Entity

EF Core Find method finds a record with the given primary key values. If the entity is already in the context (because of a previous query), then the Find method returns it. The Find method sends the query to the database if it does not find it in the context.

Syntax

Example

Read more Find method in EF Core

Update a Single Record

To Update the entity, First, we query and get the entity from the database, make the necessary changes, and then call the SaveChanges to persist the changes in the database.

Example

Update Multiple Records

Similarly, query multiple records from the database, update the records and call the saveChanges.

Read more from Update/Modify Records in EF Core

Deleting a Single Record

Remove method is 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.

Syntax

Example

Deleting a Multiple Record

RemoveRange 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

Syntax

Example

Read more from Delete Records in EF Core

Reference

DbSet

Leave a Comment

Your email address will not be published.

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

Scroll to Top