EF Core : Update column only for modified properties from disconnected entity

Vernou

I am using Entity Framework Core 3.1 with SQL Server.

I search how update column only for modified properties of a disconnected entity like :

public void UpdateOrderCustomer(int orderId, string customerName)
{
    var order = new Order { Id = orderId };

    using(var context = new MyDbContext())
    {
        context.Update(order);
        order.Customer = customerName;
        context.SaveChanges();
    }
}

But this updates all order's column.

One solution is to load entity before update properties like :

public void UpdateOrderCustomer(int orderId, string customerName)
{
    using(var context = new MyDbContext())
    {
        var order = context.Orders.Single(o => o.Id == orderId);
        order.Customer = customerName;
        context.SaveChanges();
    }
}

But to load the entity, this executes an additional useless query.

I hoped there would be a BeginTracking method, but I haven't found this or any similar functionality.

How to update columns only for modified properties from a disconnected entity?

johnny 5

You can update a single property or a deattached entity like so:

public void ChangePassword(int userId, string password)
{
  var user = new User() { Id = userId, Password = password };
  using (var db = new MyEfContextName())
  {
    db.Users.Attach(user);
    db.Entry(user).Property(x => x.Password).IsModified = true;
    db.SaveChanges();
  }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to prevent a column update in EF Core 3.1?

分類Dev

Update data in existing column with EF Core Migration

分類Dev

AutoMapper don't work with entity EF Core

分類Dev

EF Core: Get Authenticated username in Shadow Properties

分類Dev

Conditional navigation properties in Entity Framework Core

分類Dev

.net core entity framework (EF Core) table naming convention

分類Dev

ef core - update composite primary key

分類Dev

How to update EF Core Tools to the latest version

分類Dev

Select only specific fields with Linq (EF core)

分類Dev

Update an entity using entity framework while ignoring some properties

分類Dev

EF Core - dropping a column workaround (sqlite)

分類Dev

Only perform update if column exists

分類Dev

Core Data: display only objectatindex entity

分類Dev

Syncing only one entity in Core Data to iCloud

分類Dev

difference between EF and Entity-SQL when working with .net core

分類Dev

EF Core property value conversion not working in a derived entity configuration

分類Dev

Including only Id of related entity in entity framework core

分類Dev

Why projection is not including navigation nested properties in EF Core 2.0?

分類Dev

How to make inverse properties in ef core not return null but empty list

分類Dev

EF Core CosmosDb not saving child properties of inheritance hierarchy

分類Dev

When i want don't modified one Attributes,It's not work,question for ef core

分類Dev

Stop EF Core from using Merge to Insert

分類Dev

Column to be modified is not an identity column

分類Dev

The UPDATE statement conflicted with the FOREIGN KEY constraint in EF Core

分類Dev

Scaffold-DbContext Stored Procedures only in EF Core

分類Dev

Php mysql to update only empty column

分類Dev

Does EF Core allow a unique column to contain multiple nulls?

分類Dev

EF Core: Invalid column name 'companyId1'

分類Dev

postgresql update column from select

Related 関連記事

  1. 1

    How to prevent a column update in EF Core 3.1?

  2. 2

    Update data in existing column with EF Core Migration

  3. 3

    AutoMapper don't work with entity EF Core

  4. 4

    EF Core: Get Authenticated username in Shadow Properties

  5. 5

    Conditional navigation properties in Entity Framework Core

  6. 6

    .net core entity framework (EF Core) table naming convention

  7. 7

    ef core - update composite primary key

  8. 8

    How to update EF Core Tools to the latest version

  9. 9

    Select only specific fields with Linq (EF core)

  10. 10

    Update an entity using entity framework while ignoring some properties

  11. 11

    EF Core - dropping a column workaround (sqlite)

  12. 12

    Only perform update if column exists

  13. 13

    Core Data: display only objectatindex entity

  14. 14

    Syncing only one entity in Core Data to iCloud

  15. 15

    difference between EF and Entity-SQL when working with .net core

  16. 16

    EF Core property value conversion not working in a derived entity configuration

  17. 17

    Including only Id of related entity in entity framework core

  18. 18

    Why projection is not including navigation nested properties in EF Core 2.0?

  19. 19

    How to make inverse properties in ef core not return null but empty list

  20. 20

    EF Core CosmosDb not saving child properties of inheritance hierarchy

  21. 21

    When i want don't modified one Attributes,It's not work,question for ef core

  22. 22

    Stop EF Core from using Merge to Insert

  23. 23

    Column to be modified is not an identity column

  24. 24

    The UPDATE statement conflicted with the FOREIGN KEY constraint in EF Core

  25. 25

    Scaffold-DbContext Stored Procedures only in EF Core

  26. 26

    Php mysql to update only empty column

  27. 27

    Does EF Core allow a unique column to contain multiple nulls?

  28. 28

    EF Core: Invalid column name 'companyId1'

  29. 29

    postgresql update column from select

ホットタグ

アーカイブ