Add Record / Add Multiple Records In Entity Framework

In this tutorial let us look at how to add a record, add multiple records to the database. Before inserting records into the database, we must add the entities to the context first. To do that we use the Add & AddRange methods. Once the records are added to the context, we can call the SaveChanges method, which sends the insert query to the database. The EF also takes care of updating identity values generated in the database in the entity. We also show you how to add related entities or data.

Source Code:
The source code of this project available in GitHub.

Add Single Record

The code below creates a new instance of the Department object. Then it uses the Add method of the DbSet to add the newly created Department entity to the DbContext. Note that the Add method adds the new entity in Added State. Finally, we call the SaveChanges method to insert the new Department record into the database.

The DepartmentID is an identity field. The database generates its value when we insert the record into the database. The EF retrieves the newly inserted value of DepartmentID and updates the DepartmentID in the entity. This will keep the entity in sync with the database.

The SQL Query of the above method is as follows. Note the select statement, which retrieves the DepartmentID

Whenever we add an entity using the Add or AddRange method, the context marks the state of the entity as added. Hence when we call the SaveChanges Context will create an insert query and sends it to the database.

AddRange

You can add multiple records or multiple objects using the AddRange method of DbSet as shown in the following code. The code creates a list of department objects and inserts two new departments to the list. We add the list to the context using the AddRange method.

The EF will save only those records, which you add to the context using the Add method or AddRange method.

For Example, consider the following example. We retrieve all the Departments into the deps List. We create two more Departments (Dept3 & Dept4) and add it the deps list. In the next line, we add the Dept5 using the Add method.

This code only inserts the Dept5 to the database. The Dept3 & Dept4 are not added as EF is not aware of them as they are not added to the context.


Best Entity Framework Books
The Best Entity Freamework Books, which helps you to get started with Entity Framework  

The Context will track only those entities, which it retrieves from the database. It also tracks those entities which you add to the context using Add or AddRange methods. Hence it tracks all entities which are in the deps collection as they came from the database using. db.Departments.ToList(). But later we added two more departments to deps directly. The Context is not aware of these new records. Hence when the context calls SaveChanges, it does not create an Insert query for them.

Now, consider the following scenario. First, we retrieve all the Departments into the deps List. The List contains 6 records. We add two more records making it 8 records. Now we use the AddRange method and then call the SaveChanges method

The SaveChanges inserts 8 records to the database. Although the six records already exist in the database. The Add or AddRange does not check if the records exist or not. They just create a new entity and inserts them into the context and make their status as Added, which results in an insert query when you invoke SaveChanges

Entity Framework always ignores the value you provide in the Identity column. It does not include it in the insert query.

Adding Related Entities

It is a very common scenario that you may want to add related entities. There are few ways you can do that.

The following example code creates a new Employee. It then creates a new Department . The newly created Employee is added to the Employees Navigation property.

Now, when we add the new dep to the context using the Add method it also adds the Employee automatically. Hence SaveChanges will insert a new employee and department to the database.

Or in this case, we add two employees to Dep and then add the Department to the Context.

Connected & Disconnected Scenario

In the last tutorial, we discussed persistence in the entity framework. The connected or disconnected scenario has no role to play while adding an entity to the context. Because we need to use the Add & AddRange to insert them into the context, Hence they automatically become connected to context.

References

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