-
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
Owned Type should support immutable value object behavior #10307
Comments
Also, any guidance on how to actually detach the owned type (as the exception instructs) would be helpful. Not finding anything in the docs that would cover this. Tried this (in my repository):
But got the same behavior/exception. |
@ardalis calling |
@AndriySvyryd thanks! I tried this but get the same error:
My repository works with a Principal (aggregate), which has a list of Rates (of type PremiumRate). So, to detach the EffectiveDateRange value object/owned type I need to do it as shown above (I think). But, it still doesn't work. :( |
Do I need to detach it before I change it, or just before I tell the dbcontext to SaveChanges? |
It needs to be detached before it is changed. |
Ok, thanks. That got me a new error, that hopefully I can track down:
|
@ardalis You probably need to set the |
Ok, thanks. Doing that in my Repository Update method fixes the issues for actual updates, but fails on inserts with
That's fine, so I just need a way to determine whether my PremiumRate is new or pre-existing. Normally I would check its ID to see if it is default/0, but my type doesn't have an ID. Is there a way to get to the "shadow" ID, or do you have another suggestion for me to know whether the entry is Added vs. Modified (when saving a parent object that has a collection of items)? Thanks! |
You can check the |
@AndriySvyryd I'm pretty close, but I seem to be running into a problem if I load the same parent entity again after having saved changes to it (and its owned entity). When I attempt to access the owned entity's State to detach it in my GetById() method, it gives me an error saying I should "detach the previous owned entity entry first." That's exactly what I'm trying to do on the line that blows up... See screenshot: Thanks again for your help on this. Note that this only happens on the second call to this method. |
@ajcvickers Note that immutable instances for owned entities doesn't appear to have anything to do with #7586 (readonly entities). These values can still be changed in the database, they just don't have property setters that can be used to change the instance (just like |
@ardalis That exception probably means that the entity you are trying to get the entry for is already detached. This might be happening if you detached the owned entity, but didn't replace it with a new instance. |
I'm using owned types with a simple value object:
My primary concern is with this bit from the exception message:
When replacing owned entities modify the properties without changing the instance or detach the previous owned entity entry first.
The advice to modify the properties goes against having immutable value objects, which take in their state through their constructors and only provide getter properties (no setters). This scenario should be supported out of the box without having to go through workarounds like detaching things.
When I'm using this type, I'm getting this exception:
If you are seeing an exception, include the full exceptions details (message and stack trace).
Steps to reproduce
Further technical details
EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.InMemory
Operating system: Win10
IDE: VS2017 15.4.4
The text was updated successfully, but these errors were encountered: