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

Detecting if an owned entity was changed #16513

Closed
strpipe opened this issue Jul 8, 2019 · 5 comments · Fixed by #21259
Closed

Detecting if an owned entity was changed #16513

strpipe opened this issue Jul 8, 2019 · 5 comments · Fixed by #21259
Assignees
Labels
area-change-tracking breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@strpipe
Copy link

strpipe commented Jul 8, 2019

Description

When I have an entity that ownes another entity and access the IsModified-Property on the corresponding MemberEntry for the owned type, after modifying it, it always returns false.

We wanted to use the IsModified-Property to detect whether a property has changed and create change sets to later revert the entity to it's former state. It is working for all property entries but not with owned ones.

Is there a way to detect if an owned entity was changed using EF Core?

Steps to reproduce

I've included an ASP.Net MVC Application. The logic to reproduce the scenario is inside of the HomeController inside of the Index-Action
owned_entity_types_is_modified_asp_net_core_mvc.zip

Further technical details

EF Core version: 2.1.1
Database Provider: In-Memory
Operating system: Windows 10 Pro - Version 1809
IDE: Visual Studio 2017 15.9.7

@ajcvickers
Copy link
Member

ajcvickers commented Jul 8, 2019

Note for triage: this is interesting because in the code below, the change to the owned property is a delete/add operation, not an update operation. The IsModified call is being made on the relationship, and is checking if the FK property is modified, which it is not.

Maybe if the entity state is Added, then the relationship should report as modified?

Post post = new Post();
post.Address = new Address {Street = "At the mall", Zip = "01187"};
context.Posts.Add(post);
context.SaveChanges();

Address newAddress = new Address {Street = "Updated", Zip = "Updated"};
post.Address = newAddress;

MemberEntry memberEntry = context.Entry(post).Member(nameof(Post.Address));
Console.WriteLine(memberEntry.IsModified);

@ajcvickers ajcvickers added this to the Backlog milestone Jul 8, 2019
@ajcvickers ajcvickers self-assigned this Jul 8, 2019
@ajcvickers
Copy link
Member

Notes from triage: putting this on the backlog as a bug. However, note that in this case it will be an update because of the way owned types work. But if the relationship is identifying, then we don't mark the FK as modified. We should investigate this space.

@strpipe
Copy link
Author

strpipe commented Jul 12, 2019

I may have found other points of interest. They are not about owned entity types but may still be related.
I created a new sample project, there is code inside three actions of the home controller. Two fail, one passes. If it's not related I could open another issue, however, feel free to that on your own. Please let me know how to proceed. Thank you.
is_modified_asp_net_core_mvc_v2.zip

@ajcvickers
Copy link
Member

I investigated this some more and I now understand why this was labeled a bug. Collection navigations already have this behavior--that is, Deleted or Added related entities indicate that the navigation property is modified. So this change brings reference navigations inline, which I think is a bug. Nevertheless, I think it makes sense to document this as a breaking change.

ajcvickers added a commit that referenced this issue Jun 14, 2020
…d entities

Fixes #16513

I investigated this some more and I now understand why this was labeled a bug. Collection navigations already have this behavior--that is, Deleted or Added related entities indicate that the navigation property is modified. So this change brings reference navigations inline, which I think is a bug. Nevertheless, I think it makes sense to document this as a breaking change.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 14, 2020
ajcvickers added a commit that referenced this issue Jun 15, 2020
…d entities (#21259)

Fixes #16513

I investigated this some more and I now understand why this was labeled a bug. Collection navigations already have this behavior--that is, Deleted or Added related entities indicate that the navigation property is modified. So this change brings reference navigations inline, which I think is a bug. Nevertheless, I think it makes sense to document this as a breaking change.
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview7 Jun 22, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview7, 5.0.0 Nov 7, 2020
@voroninp
Copy link

voroninp commented Apr 4, 2022

@ajcvickers ,

VehicleHandoverTimes is an owned entity. When handover is cancelled property is set to null.
How can I detect the change? How can I access the original value?
image

EF Core 6.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants