There are two ways to update records using EntityFramework Core:
In a connected scenario, you can update an entity by retrieving it from the database, making the necessary changes, and then calling the
SaveChanges() method.
In a disconnected scenario, you can update an entity by first attaching it to the context, setting its state to
Modified, and then calling the SaveChanges() method.
Here is an example of how to update an entity in a connected scenario:
C#
using Microsoft.EntityFrameworkCore;
namespace MyApp
{
public class Program
{
public static void Main(string[] args)
{
// Create a DbContext instance.
var context = new MyContext();
// Retrieve the entity to update.
var product = context.Products.Find(1);
// Make the changes to the entity.
product.Name = "New Product Name";
// Save the changes.
context.SaveChanges();
}
}
}
Here is an example of how to update an entity in a disconnected scenario:
C#
using Microsoft.EntityFrameworkCore;
namespace MyApp
{
public class Program
{
public static void Main(string[] args)
{
// Create a DbContext instance.
var context = new MyContext();
// Retrieve the entity to update.
var product = context.Products.Find(1);
// Attach the entity to the context.
context.Attach(product);
// Set the entity's state to Modified.
product.State = EntityState.Modified;
// Save the changes.
context.SaveChanges();
}
}
}
In both cases, the SaveChanges() method will update the entity in the database.
Here are some additional considerations when updating records using Entity Framework Core:
If you are updating a related entity, you will need to update the parent entity as well.
If you are updating a large number of records, you may want to use the
BulkInsert() or BulkUpdate() methods instead of calling
SaveChanges() multiple times.
You can use the Include() method to specify which properties of an entity you want to update. This can improve performance by reducing the amount of data that needs to be transferred between the database and the application.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are two ways to update records using Entity Framework Core:
SaveChanges()method.Modified, and then calling theSaveChanges()method.Here is an example of how to update an entity in a connected scenario:
C#
Here is an example of how to update an entity in a disconnected scenario:
C#
In both cases, the
SaveChanges()method will update the entity in the database.Here are some additional considerations when updating records using Entity Framework Core:
BulkInsert()orBulkUpdate()methods instead of callingSaveChanges()multiple times.Include()method to specify which properties of an entity you want to update. This can improve performance by reducing the amount of data that needs to be transferred between the database and the application.