-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Can't find how to add to an owned collection without modifying the rest of the items #20803
Comments
@mooseburger There are different ways of handling graphs of disconnected entities, as discussed in the docs: https://docs.microsoft.com/en-us/ef/core/saving/disconnected-entities. It's hard to know without actual code, but it seems like for your case using Attach on the parent followed by setting state on the children should work. Alternately, you could use These blog posts may also be useful: |
So I have this:
And this is configured like this:
But then, that leaves it up to me to determine which (if any) of the children, were modified no? There's no way to have ChangeTracker do that? |
@mooseburger If you want control over that, then use |
It's not that I wish I had control over it, it's that I wish EF Core would do it for me. TrackGraph appears to leave it up to me to devise a callback in which to determine whether a comment has been modified. I decided to do something like this within an override of SaveChangesAsync:
This prevents modification of comments, app-wide, but I would have preferred to throw an exception. |
@mooseburger If there is some way that EF is behaving differently than your expectations, then please attach a small, runnable project or post a small, runnable code listing that reproduces this behavior. We're happy to consider enhancements, but we need to be talking about a concrete behavior with just code snippets. |
It's just that when I saw something called ChangeTracker, I assumed it would detect changes in all cases. I guess it makes sense that it cannot do this in a web app scenario, where the entity being updated is untracked since it came in through a controller, and not from the DB context. |
@mooseburger We have an issue on the backlog for making it easier to do this out-of-the-box with disconnected entities: #5536. |
Thanks, that does seem like it would help in this situation, were it implemented. |
Bug: An unmodified owned entity in a collection will still enter modified state if Update is called on its parent entity.
I have an owned type called
Comment
. Several entities in my application have collections of Comments. I have a requirement that aComment
can never be modified. I thought to use theDbContext.ChangeTracker
to throw an exception whenever a comment is in theModified
state. But the problem is that if Update is called on the parent of the comment collection, every comment that was there is changed toModified
state, even if no change actually occurred on the comment.I got around this by using
Attach
on the parent entity, instead ofUpdate
, then setting the resultingEntityEntry
'sState
toModified
. This fixed the issue with comments, but it broke another owned entity,Address
. There are entities that have twoAddress
references (postal and residential), but with theAttach
workaround, these addresses have stopped updating. Looking at the docs, I can't figure out how to implement this comment requirement using EF core. It appears theChangeTracker
is useless for this purpose as far as I can tell.Steps to reproduce
Further technical details
EF Core version: 3.1
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.0)
Operating system: Windows 10
IDE: (e.g. Visual Studio 2019 16.5.4)
The text was updated successfully, but these errors were encountered: