Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] CurrentValues.SetValues does not update owned types, but any other primitive properties are updated #25032

Closed
rafaelfgx opened this issue Jun 3, 2021 · 2 comments

Comments

@rafaelfgx
Copy link

rafaelfgx commented Jun 3, 2021

Hi guys,

I have a problem with owned types. When CurrentValues.SetValues method is called to partially update an entity, primitive properties are updated correctly, but owned types are not.

Entity

public sealed class Customer
{
    public Customer() { }

    public Customer(long id, Name name)
    {
        Id = id;
        Name = name;
    }

    public long Id { get; private set; }

    public Name Name { get; private set; }
}

Value Object (Owned Type)

public sealed record Name(string FirstName, string LastName);

Entity Type Configuration

public sealed class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
    public void Configure(EntityTypeBuilder<Customer> builder)
    {
        builder.ToTable(nameof(Customer));

        builder.HasKey(customer => customer.Id);

        builder.Property(customer => customer.Id).IsRequired().ValueGeneratedOnAdd();

        builder.OwnsOne(customer => customer.Name, customerName =>
        {
            customerName.Property(name => name.FirstName).HasColumnName(nameof(Name.FirstName)).IsRequired().HasMaxLength(100);

            customerName.Property(name => name.LastName).HasColumnName(nameof(Name.LastName)).IsRequired().HasMaxLength(250);
        });
    }
}

BUG: CurrentValues.SetValues does not update owned types, but any other primitive properties are updated.

var customer = Set.Find(1L);

var newCustomer = new
{
    Id = 1L,
    Name = new
    {
        FirstName = "Updated Name"
    }
}

_context.Entry(customer).CurrentValues.SetValues(newCustomer);
@rafaelfgx rafaelfgx changed the title Bug: CurrentValues.SetValues does not update owned types, but any other primitive properties are updated [BUG] CurrentValues.SetValues does not update owned types, but any other primitive properties are updated Jun 3, 2021
@AndriySvyryd
Copy link
Member

Use context.Entry(customer).Reference("Name").TargetEntry.CurrentValues.SetValues(newCustomer.Name);

Duplicate of #18366

@AndriySvyryd
Copy link
Member

I haven't tested it, but something like this:

var entry = context.Entry(entity);
var entityType = entry.Metadata;
foreach (var navigation in entityType.GetNavigations())
{
    if (navigation.IsOnDependent
        || navigation.IsCollection
        || !navigation.ForeignKey.IsOwnership)
    {
        continue;
    }

    entry.Reference(navigation.Name).TargetEntry.CurrentValues.SetValues(
        navigation.GetGetter().GetClrValue(newEntity));
}

And you probably will want to invoke this recursively.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants