Docs Menu
Docs Home
/ / /
EF Core Provider
/

Write Data to MongoDB

On this page

  • Overview
  • Transactional Write Operations
  • Insert
  • Insert One Entity
  • Insert Multiple Entities
  • Update
  • Delete
  • Delete One Entity
  • Delete Multiple Entities
  • Additional Information

Entity Framework Core allows you to work with data in your application without explicitly running database commands. You can insert, update, or delete data within your application and persist those changes to MongoDB by using the SaveChanges() or SaveChangesAsync() method.

When you call the SaveChanges() or SaveChangesAsync() method, the EF Core Provider automatically detects any changes made to your data and runs the necessary commands to update the database by using the MongoDB Query API.

In this guide, you can see examples of how to perform common write operations on an application configured to use the EF Core Provider.

Tip

To learn how to configure an application to use the EF Core Provider, see Configure the EF Core Provider.

The SaveChanges() and SaveChangesAsync() methods are transactional by default. This means that if an error occurs during an operation, the provider rolls back any changes made to the database. Because of this, your application must be connected to a transaction-capable deployment of MongoDB server, such as a replica set.

You can disable automatic transactions in the SaveChanges() and SaveChangesAsync() methods by setting the AutoTransactionBehavior property to AutoTransaction.Never on your DbContext subclass during application setup. However, we do not recommend disabling this feature. Doing so causes any concurrency changes or operation failures during the save operation to leave the database in an inconsistent state.

The following example shows how to disable automatic transactions in the SaveChanges() and SaveChangesAsync() methods:

dbContext.Database.AutoTransactionBehavior = AutoTransactionBehavior.Never;

Warning

Disabling automatic transactions can lead to data inconsistencies. We recommend that you do not disable this feature.

You can use the Add() method to insert a single entity into your collection, or you can use the AddRange() method to insert multiple entities at once.

The Add() method accepts a single entity of the same type that you specified on the DbSet instance that you are modifying.

The following code uses the Add() method to add a new Planet object to the DbSet called Planets. It then calls the SaveChanges() method to insert that entity into the MongoDB collection.

db.Planets.Add(new Planet()
{
name = "Pluto",
hasRings = false,
orderFromSun = 9
});
db.SaveChanges();

The AddRange() method accepts an array of entities that you want to add to the DbSet.

The following code uses the AddRange() method to add an array of Planet objects to the DbSet called Planets. It then calls the SaveChanges() method to insert those entities into the MongoDB collection.

var planets = new[]
{
new Planet()
{
_id = ObjectId.GenerateNewId(),
name = "Pluto",
hasRings = false,
orderFromSun = 9
},
new Planet()
{
_id = ObjectId.GenerateNewId(),
name = "Scadrial",
hasRings = false,
orderFromSun = 10
}
};
db.Planets.AddRange(planets);
db.SaveChanges();

To update an entity, first retrieve the entity that you want to update. Then make the changes to that entity. The provider tracks any changes made to the entity, such as setting properties or adding and removing items from fields with list values. To save the update to MongoDB, call the SaveChanges() method. The EF Core Provider compares the updated entity with a snapshot of the entity before the change and automatically updates the collection by using the MongoDB Query API.

The following code retrieves an entity in which the name value is "Mercury", then updates the name field. The code then calls the SaveChanges() method to persist that change to the collection.

var planet = db.Planets.FirstOrDefault(p => p.name == "Mercury");
planet.name = "Mercury the first planet";
db.SaveChanges();

You can use the Remove() method to delete a single entity from your collection, or the RemoveRange() method to delete multiple entities at once.

The Remove() method accepts a single entity of the same type that you specified on the DbSet instance that you are modifying.

The following code removes a Planet entity in which the name value is "Pluto". It then calls the SaveChanges() method to delete that entity from the MongoDB collection.

var planet = db.Planets.FirstOrDefault(p => p.name == "Pluto");
db.Planets.Remove(planet);
db.SaveChanges();

The RemoveRange() method accepts an array of entities to remove from the DbSet.

The following code finds two Planet entities and adds them to an array. It then uses the RemoveRange() method to remove both entities from the DbSet. Finally, it uses the SaveChanges() method to remove those entities from the MongoDB collection.

var pluto = db.Planets.FirstOrDefault(p => p.name == "Pluto");
var scadrial = db.Planets.FirstOrDefault(p => p.name == "Scadrial");
var planets = new[] { pluto, scadrial };
db.Planets.RemoveRange(planets);
db.SaveChanges();

To learn more about the methods discussed in this guide, see the following .NET API documentation links:

  • SaveChanges()

  • Add()

  • AddRange()

  • Remove()

  • RemoveRange()

Back

Query Data