efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Using the same owned type multiple times in same entity

Open ccaymaz opened this issue 6 years ago • 4 comments

I have "Customer" owned type and used multiple time in the same entity. The problem is second "Customer" owned type is not tracked by entity. It issue started happening after upgrading EF 2.1 to 3.1

I might miss some documentation about owned types but I couldn't find anything about it.

Steps to reproduce


    public class Order : Entity<int>
    {
        public Customer Customer1 { get; private set; }
        public Customer Customer2 { get; private set; }
    }

    public class Customer
    {
        public int? ContactId { get; private set; }
        public Contact Contact { get; private set; }

        public void UpdateContact(int? contactId)
        {
            this.ContactId = contactId;
        }
    }

        public void Configure(EntityTypeBuilder<Order> builder)
        {
            builder.ToTable("Orders", "Application");

            builder.OwnsOne(
                o => o.Customer1,
                sa =>
                {
                    sa.Property(e => e.ContactId)
                        .HasColumnName("PrimaryContactId");

                    sa.HasOne(e => e.Contact)
                        .WithMany()
                        .HasForeignKey(s => s.ContactId)
                        .HasConstraintName("FK_Orders_Contacts_1");
                });

            builder.OwnsOne(
                o => o.Customer2,
                sa =>
                {
                    sa.Property(e => e.ContactId)
                        .HasColumnName("SecondaryContactId");

                    sa.HasOne(e => e.Contact)
                        .WithMany()
                        .HasForeignKey(s => s.ContactId)
                        .HasConstraintName("FK_Orders_Contacts_2");
                });
        }

//Get order with findAsync here
order.Customer1.UpdateContact(primaryContactId);
order.Customer2.UpdateContact(secondaryContactId);
//Save context here

When I check the entity ChangeTracker, second customer is not tracked. Customer1 state is Modified but Customer2 even not in the ChangeTracker.

Isn't weird behavior? Or am I missing something?

Further technical details

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET Core 3.1.1) Operating system: IDE: (e.g. Visual Studio 2019 16.4.2)

ccaymaz avatar Jan 26 '20 19:01 ccaymaz

@ccaymaz Please attach a small, runnable project or post a complete code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Jan 27 '20 16:01 ajcvickers

@ajcvickers Thanks you for your response. I wrote a small console log application and everything is working. So, that shows my mapping somehow incorrect. I will go back to my code and find the issue. If I can repeat the problem, I will get back to you with this thread. I am closing this thread. Thanks.

ccaymaz avatar Jan 29 '20 20:01 ccaymaz

Bugfix/feature still needed :)

PhilippDehner avatar Feb 06 '26 12:02 PhilippDehner

@PhilippDehner see above - we never got a minimal repro.If you're still seeing a problem here, please post one.

roji avatar Feb 06 '26 14:02 roji