question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CurrentValues.SetValues() not working for nested objects

See original GitHub issue

I have the following methods on a repository:

public async Task<T> AddAsync(T entity)
{
    await _dbContext.Set<T>().AddAsync(entity);
    return entity;
}

public async Task UpdateAsync(T entity)
{
    var selected = await _dbContext.Set<T>().FindAsync(entity.Id);
    if (selected is not null)
        _dbContext.Entry(selected).CurrentValues.SetValues(entity);
}

I have the following entity:

public abstract class AuditableEntity<TId> : IAuditableEntity<TId>
{
    public TId Id { get; set; }
    public string? CreatedBy { get; set; }
    public DateTime? CreatedOn { get; set; }
    public string? LastModifiedBy { get; set; }
    public DateTime? LastModifiedOn { get; set; }
}

public class Customer : AuditableEntity<int>
{
    public int Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
    public Contact Contact { get; set; }
    public int LeadSource { get; set; }
    public IList<Order> Orders { get; set; }
}

public class Address
{
    public string? Number { get; set; }
    public string? Postcode { get; set; }
    public string? Street { get; set; }
    public string? Locality { get; set; }
    public string? Town { get; set; }
    public string? County { get; set; }
    public string? Country { get; set; }
}

public class Contact
{
    public string? Mobile { get; set; }
    public string? Home { get; set; }
    public string Email { get; set; }
}

DB context configures the following behaviour for Customer:

builder.Entity<Customer>()
    .OwnsOne(o => o.Address);
builder.Entity<Customer>()
    .OwnsOne(o => o.Contact);

Calling AddAsync successfully adds the entity to the DB, calling UpdateAsync will update only the top level non nested properties.

When I try to update the entity with a new Address or Contact, the values aren’t updated. But if I update FirstName it works fine. Should this be working as I expect or am I missing something?

Provider and version information

EF Core version: 6.0.7 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Windows 11 IDE: 2022.1.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ajcvickerscommented, Jul 29, 2022

@ScottKane SetValues works on a single entity instance. It does not set values for related entity instances.

0reactions
ScottKanecommented, Jul 29, 2022

@ajcvickers well I’m using it to do that and it definitely is updating the related entity. I’m assuming this is because a Customer owns an Address so it’s effectively treaded as part of the entity? I’m never using this to update a one to many relationship as there is a separate API for the child entity to be updated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fail to update Entity with nested value objects - ...
My issue is that when updating the entity along with the value objects, the entity with the parent value object get updated but...
Read more >
Working with property values - EF6
Working with property values in Entity Framework 6. ... CurrentValue; // Get the nested State complex object using chained calls var state1 ...
Read more >
Updating existing rows is not working by 'set values' when ...
Solution 1. How about making it easier. Don't use SetValues, instead, just set the value of the property you want to change.
Read more >
Configuring and Updating nested value objects in Entity ...
Known Issues: - The method UpdateChildValueObjects sets the values of the non updated fields of the entity to what they where in the...
Read more >
useForm
Enables form-level validation, uses the specified schema to validate the fields. The schema can be either valid vee-validate global validators or functions or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found