EF Core Disconnected Entities

A disconnected entity is an entity that is not being tracked by the current DbContext.

Use methods such as Add(), Update(), Remove(), Attach(), or their range equivalents to tell EF Core whether disconnected entities should be inserted, updated, or deleted when SaveChangesAsync() is called.

🀯 Wait... Entity Framework Extensions Is FREE?!

Yes, really! Get a $0 perpetual commercial license with ALL database providers. Not a trial. No subscription. For eligible new customers under $1M revenue.

See How to Get It FREE →

Insert Disconnected Entities with AddRange

Use Add() or AddRange() when the disconnected entities are new and should be inserted.

language-csharp
|
using var context = new AppDbContext();

var keyboard = new Product { Name = "Mechanical Keyboard", Price = 120 };
var mouse = new Product { Name = "Wireless Mouse", Price = 40 };
var monitor = new Product { Name = "27-inch Monitor", Price = 250 };

context.Products.AddRange(keyboard, mouse, monitor);

await context.SaveChangesAsync();

This inserts the three products in the database.

AddRange() tells EF Core to track the entities as Added.

For more details about this insert workflow, see Adding Data.

Update Disconnected Entities with UpdateRange

Use Update() or UpdateRange() when the disconnected entities already exist in the database and should be updated.

language-csharp
|
using var context = new AppDbContext();

var keyboard = new Product
{
    Id = 1,
    Name = "Mechanical Keyboard",
    Price = 120
};

var mouse = new Product
{
    Id = 2,
    Name = "Wireless Mouse",
    Price = 45
};

var monitor = new Product
{
    Id = 3,
    Name = "27-inch Monitor",
    Price = 250
};

context.Products.UpdateRange(keyboard, mouse, monitor);

await context.SaveChangesAsync();

This marks all three products and all their properties as Modified.

As a result, EF Core assumes every property has changed and includes all mapped columns in the generated UPDATE statement, even if only some values were actually changed.

UpdateRange() is useful for disconnected workflows, but it does not perform change tracking or determine which properties changed.

For more details about this update workflow, see Updating Data.

Update Selected Properties with AttachRange

We just saw that Update() and UpdateRange() mark all properties as Modified.

If you want to update only specific properties, you can use Attach or AttachRange() and explicitly mark the properties that should be updated.

language-csharp
|
using var context = new AppDbContext();

var product = new Product
{
    Id = 10,
    Price = 45
};

context.Products.Attach(product);

context.Entry(product)
    .Property(p => p.Price)
    .IsModified = true;

await context.SaveChangesAsync();

This attaches the entity and marks only the Price property as Modified.

As a result, EF Core generates an UPDATE statement that only includes the Price column.

This approach is useful in disconnected scenarios when you know exactly which properties have changed and want to avoid updating every column.

For more details about this update workflow, see Updating Data.

Delete Disconnected Entities with RemoveRange

Use Remove() or RemoveRange() when the disconnected entities should be deleted.

language-csharp
|
using var context = new AppDbContext();

var keyboard = new Product { Id = 1 };
var mouse = new Product { Id = 2 };
var monitor = new Product { Id = 3 };

context.Products.RemoveRange(keyboard, mouse, monitor);

await context.SaveChangesAsync();

This deletes the three products from the database when SaveChangesAsync() runs.

RemoveRange() tells EF Core to track the entities as Deleted.

For more details about this delete workflow, see Deleting Data.

What Are Disconnected Entities?

EF Core automatically tracks entities returned from the database by a DbContext.

In a connected workflow, the same context queries the entity, tracks it, detects changes, and saves those changes.

In a disconnected workflow, the entity is saved by a DbContext that was not already tracking it.

This is common in web applications and APIs, where data is sent to a client, modified, sent back to the server, and then saved using a new context instance.

AddRange, UpdateRange, and RemoveRange in Disconnected Scenarios

In disconnected scenarios, the method you choose tells EF Core which state should be assigned to the entity.

  • AddRange() tracks entities as Added
  • UpdateRange() tracks entities as Modified
  • RemoveRange() tracks entities as Deleted

The database operation is sent only when SaveChangesAsync() runs.

For more details about range methods such as AddRange, UpdateRange, AttachRange, and RemoveRange, see the official EF Core documentation on range methods.

Important Behavior

Disconnected entities are not being tracked by the current DbContext.

That means EF Core does not already know whether they are new, changed, or should be deleted. You need to choose the method that matches the intended operation:

  • use AddRange() for new entities
  • use UpdateRange() for existing entities that should be updated
  • use RemoveRange() for entities that should be deleted

After that, SaveChangesAsync() sends the required INSERT, UPDATE, or DELETE statements to the database.

A key value is especially important for updates and deletes. Without a valid key, EF Core cannot reliably target the existing row.

EF Core can only track one instance of an entity with a given primary key value in the same DbContext. Using a short-lived context for each unit of work helps avoid tracking conflicts.

For a general overview of saving tracked changes, see SaveChanges.

For a deeper step-by-step explanation of what EF Core does internally when changes are saved, see How SaveChanges Works.

For more about how EF Core stores entity states, see ChangeTracker.

For a deeper look at how EF Core tracks entity states before saving, see Tracking Changes of Entities.

For the official EF Core documentation about disconnected scenarios, see Disconnected entities.

How EF Core Handles Disconnected Entities

When a disconnected entity arrives in a new DbContext, EF Core has no previous tracking information for that instance.

A simplified flow looks like this:

  1. The entity is created, received, or mapped from another layer.
  2. A new DbContext starts tracking it.
  3. You call AddRange(), UpdateRange(), or RemoveRange().
  4. EF Core marks the entity as Added, Modified, or Deleted.
  5. You call SaveChangesAsync().
  6. EF Core sends the corresponding database command.

The method you call is the signal EF Core uses to decide the entity state.

When to Use Disconnected Entity Patterns

Use disconnected entity patterns when:

  • data is sent from a client and saved later by a new DbContext
  • you are working in a web application or API
  • the original context that loaded the data is no longer available
  • you already know whether the incoming entities should be inserted, updated, or deleted
  • you want to save one or more incoming entities as part of the same unit of work

Common examples include:

  • inserting several new products from a request
  • updating products received from an API payload
  • deleting selected products by key
  • saving changes from a service layer where the original query context has already been disposed

When Not to Use Disconnected Entity Patterns

Disconnected patterns are not always the best choice.

Avoid them when:

  • the entity is already tracked by the current DbContext
  • you only need to update or delete rows that match a query
  • you need high-performance operations for thousands or millions of rows
  • you only want to update one specific property
  • you are not sure whether the entity is new or existing

For connected workflows, see Saving Data in Connected Scenario.

For set-based updates, see ExecuteUpdate.

For set-based deletes, see ExecuteDelete.

For high-volume operations, see Bulk Insert, Bulk Update, and Bulk Delete.

⚑ Delete Thousands of Entities Efficiently

Deleting large datasets shouldn't take forever. BulkDelete helps you remove thousands of records without the overhead of regular SaveChanges.

Explore BulkDelete →

Common Pitfalls

Be careful with the following mistakes.

Using AddRange for Existing Entities

AddRange() tells EF Core that the entities are new.

If the entities already exist in the database, EF Core may try to insert duplicate rows.

Use UpdateRange() when the entities already exist and should be updated.

Using UpdateRange When Only One Property Changed

UpdateRange() marks entities as Modified.

In disconnected scenarios, EF Core usually does not know which properties actually changed before the entity was attached.

If you only want to update one property, use a more explicit pattern such as Attach() with Property(...).IsModified = true.

For more information, see Updating Data.

Deleting Entities Without a Key

RemoveRange() needs valid key values to know which rows should be deleted.

language-csharp
|
var product = new Product();

context.Products.Remove(product);

await context.SaveChangesAsync();

In this example, the key value is missing, so EF Core cannot reliably target the intended row.

A safer disconnected delete provides the key value.

language-csharp
|
var product = new Product
{
    Id = 10
};

context.Products.Remove(product);

await context.SaveChangesAsync();

For more information, see Deleting Data.

Tracking Multiple Instances with the Same Key

EF Core can only track one instance of an entity with a given primary key value in the same DbContext.

This can happen if one instance is already tracked and another instance with the same key is attached later.

A short-lived DbContext per unit of work helps reduce this problem.

Forgetting SaveChangesAsync

AddRange(), UpdateRange(), and RemoveRange() only change entity states in the ChangeTracker.

You must call SaveChangesAsync() to persist the insert, update, or delete operation in the database.

External Resources - Disconnected Entities

The following resources were selected because each one supports an important part of the disconnected entity workflow: what disconnected entities are, how EF Core assigns entity states, and why the ChangeTracker matters when a new DbContext saves incoming entities.

Video 1 - Eager Loading, Lazy Loading, Explicit Loading, and Disconnected Entities

This Dot Net Tutorials video covers several EF Core topics, but the most relevant part for this article starts near the end, where it explains disconnected entities and saving disconnected data.

That section shows how EF Core can work with entities that are not already tracked by the current DbContext, and how states such as Added, Modified, and Deleted can be assigned before saving changes.

It is useful as a complementary resource if you want to see a practical explanation of disconnected entities without going into advanced graph-tracking scenarios.

Key sections:

  • 1:56:00 β€” Introduction to disconnected entities
  • 1:57:00 β€” Updating a disconnected entity
  • 1:58:30 β€” Using DbContext.Entry() with disconnected entities
  • 2:00:57 β€” Setting the entity state to Added or Modified based on the key value
  • 2:06:53 β€” Calling SaveChanges() to persist the disconnected entity state

Video 2 - Change Tracker in Entity Framework Core

Rahul Pandey explains how EF Core uses the ChangeTracker to manage entity states such as Detached, Added, Unchanged, Modified, and Deleted.

This video is useful as a supporting resource because it explains the entity states used by the ChangeTracker, especially Detached, Added, Modified, and Deleted. These states are important in disconnected scenarios because EF Core must start tracking an incoming entity and assign the correct state before SaveChanges() can generate the database command.

Key sections:

  • 1:09 β€” Overview of entity states: Detached, Added, Unchanged, Modified, and Deleted
  • 7:53 β€” Demo showing a Detached entity before it is added to the context
  • 8:09 β€” Entity state changes from Detached to Added after Add() is called
  • 11:32 β€” AsNoTracking() and no-tracking query behavior

Summary

Disconnected entities are entities that are not being tracked by the current DbContext.

Key points:

  • disconnected scenarios are common in web applications and APIs
  • EF Core needs to know whether the entity should be inserted, updated, or deleted
  • use Add() or AddRange() when disconnected entities are new
  • use Update() or UpdateRange() when disconnected entities already exist and should be updated
  • use Remove() or RemoveRange() when disconnected entities should be deleted
  • SaveChangesAsync() sends the corresponding INSERT, UPDATE, or DELETE statements to the database
  • keys are important for updating and deleting existing rows
  • EF Core can only track one instance of an entity with a given primary key value in the same DbContext

Use disconnected entity patterns when the original context is no longer available and a new context must save incoming entities.

⚑ Insert Thousands of Entities in Seconds

EF Core inserts taking too long? Use BulkInsert to save thousands of records efficiently with fewer database round trips.

Explore BulkInsert →

If you want to understand how disconnected entities fit into the broader EF Core saving workflow, these pages are the best next steps:

  • SaveChanges β€” how EF Core persists tracked changes
  • How SaveChanges Works β€” what EF Core does internally when SaveChangesAsync() saves disconnected entities after they are tracked
  • Adding Data β€” how to insert new disconnected entities with Add() or AddRange()
  • Updating Data β€” how to update disconnected entities with Update() or UpdateRange()
  • Deleting Data β€” how to delete disconnected entities with Remove() or RemoveRange()
  • Saving Data in Connected Scenario β€” how EF Core saves entities that are already tracked by the current context
  • ChangeTracker β€” how EF Core stores entity states such as Added, Modified, and Deleted
  • Tracking Changes of Entities β€” how EF Core tracks entity states before saving

FAQ

What is a disconnected entity in EF Core?

A disconnected entity is an entity that is not being tracked by the current DbContext.

This often happens when data is loaded or created in one layer, sent to a client, and later saved using a new DbContext.

How do I insert, update, or delete disconnected entities?

Use the method that matches the intended operation:

  • use Add() or AddRange() for new entities
  • use Update() or UpdateRange() for existing entities that should be updated
  • use Remove() or RemoveRange() for entities that should be deleted

After that, call SaveChangesAsync() to persist the changes in the database.

Why are keys important in disconnected scenarios?

Keys help EF Core identify the row that should be updated or deleted.

Without a valid key, EF Core cannot reliably know which existing row the disconnected entity represents.

Does UpdateRange only update changed properties?

Not always.

In disconnected scenarios, EF Core usually does not know which properties changed before the entity was attached. UpdateRange() can therefore mark the entities as Modified.

For partial updates, use a more explicit pattern such as Attach() with Property(...).IsModified = true.

Can EF Core track two instances with the same key?

No. EF Core can only track one instance of an entity with a given primary key value in the same DbContext.

Using a short-lived DbContext for each unit of work helps avoid this problem.


Date Modified: 2026-07-26
Author:

Edit this page in GitHub

Got any EF Core Question?