Support value generation with converters
Current behavior is to throw by default if a converter is associated with a property that will use value generation However, a value generator to use can be specified:
- On the property
- On the type mapping
- On a converter
A client-side generator is added to the Guid converters so that Guid key scenarios still work. We should find similar "safe" cases and make those work as well.
Original PR: https://github.com/aspnet/EntityFrameworkCore/pull/11479
Consider the Oracle case in #11559 when looking into this.
Note: when working on this, also consider #11970
Is anyone already working on this and is there an ETA? If not, would you accept PR's for this?
@twsI It's in the 3.0 milestone, which means it is tentatively planned for 3.0. I don't believe anyone is working on it right now. We would consider PRs, but it would be helpful if you could provide some details of what you plan to do before submitting a PR, since it's not clear what the behavior here should be or how it should be implemented.
Any clue if this is going to be ready in version 3.0?
@gbrantzos No. The punted-for-3.0 label means that it had to be cut from the release. The current milestone is Backlog which also means that it is not going to happen for the 3.0 release. We will re-assess the backlog following the 3.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
Is there anything we can do to push this into a release asap?
@twsI The 3.0 release is essentially done. Beyond that, you can vote for this by a thumbs-up 👍 on the issue.
I was poking around with this one to support identity value generation using a strongly typed PK that simply wraps an int value, and I was able to get it working setting the underlying metadata (thus circumventing all the checks hat are done in the SqlServerPropertyAnnotations in my case.
I'm not sure why we're rejecting cases where we have a converter, is there any technical limitation somewhere or is just a not implemented and tested now?
Server side value generation strategy should utilize provider CLR type to identify value generation as mentioned in above comment.
@ilmax - There are some positive case where it works, and many negative cases where it does not so we have blocked almost everything except we are positive scenario works. You could say that not implemented and tested scenario.
@smipatel thanks for coming back to me on this one, I would love to see the scenario I've mentioned above supported, this is essentially to allow strongly typed primary keys, IMHO it's really nice to be able to avoid the "primitive obsession" anti pattern and so far we can only use Guid or generate values ourselves, but actually I was able to have it working perfectly fine just avoiding the checks as mentioned.
Is this something the team may consider to enable?
@ilmax - We can consider it for next major release. Since the issue is marked as needs-design, can you write some design, what would be architecture or logic to figure out value generations with converters. I can present it to team and get final decision. (Also since we are working on 3.1 right now, it would be few months before we may be able to get to it.)
@smitpatel it's not super clear to me what design/architecture are you asking here so I may need some help, essentially what am I proposing is just relaxing some checks because in the simple case everything works, so I think this boils down to identify cases that doesn't work and make sure we're blocking them, while allowing the cases that work "out of box".
@ilmax The fundamental problem is changes in valid key-space and available value generation. Consider these examples:
- A Guid property being converted to a string in the database
- Here it makes sense to use the property type (Guid) to generate a value; it's reasonable to assume that all strings created in this way will be valid in the database and won't collide
- This is easy because Guids can be client-generated
- An int property being converted to a string in the database
- Here it again makes sense to use an int type to generate a value, and assume that any integer generated can be converted to an appropriate string
- However, there is no way to do (purely) client-side generation of ints, so then we can't really do this.
- Also, the database column is string, which can't generate ints and so isn't very useful.
- So what should happen here?
- A string property being converted to a Guid in the database
- Here it makes sense to generate Guids, and then assume strings in the model are in a form that can be parsed as Guid.
- But now we're using to use the column type, rather than the property type, for the generation.
There are many other combinations of property/column types, some of which have reasonable value generation available based on either the column type or model type, as well as many that don't have any reasonable way to generate a value.
So, from a behavior perspective we need to decide:
- Given an arbitrary conversion, can we come up with an approach that is reasonably safe and not unexpected?
- Assuming the answer is no, then can we instead figure out some reasonable special cases where we can do this? Will these special cases be useful?
- For other cases, what API do we need to allow the value generation to be explicitly specified?
@ajcvickers thanks for the very detailed comment, it make sense to block all the cases you're mentioning, I was just thinking about opening up few straightforward ones, like have a guid wrapper class or an short/int/long wrapper class (i.e. the DDD way) so for example have an OrderId class that's wrapping let's say a long and be able to configure a value converter and use the identity generation strategy.
Since I was able to get this one working perfectly fine circumventing all the validation, I was thinking about making this a supported feature, what was required to get this working was configuring the property with identity generation via the metadata to circumvent the checks, implement a custom ValueConverterSelector and a delegating ValueGenerator that wraps a TemporaryLongValueGenerator.
Since I really don't like using int/long/guid as my PK instead I prefer to have dedicated classes, I'd love to have this one supported, so do you think we can make checks smart enough to only open up this case and block the others?
@ilmax Possibly; that's one of the questions that need answering.
@ajcvickers I agree with @ilmax, if I understand the discussion correctly. EF should allow conversions for keys, and should validate configuration for invalid combinations of conversion and value generation. There are many cases where key generation is not used and the key is more than just a number, has some structure and needs to be validated by business code (possible example is SSN). I'd be happy to encapsulate such an ID in a class/struct.
@andriysavin Not sure I follow the motivation for your comment. Are you just agreeing that you would like to see this implemented?
@ilmax I was thinking about this again a couple of nights ago. I think the following might be reasonable:
- If the store type has some kind of value generation available, then by default use it.
- If this results in temporary values, then use the converter to convert these temp values in the normal way. This may fail, in some cases, but I think it's okay to require value generation to be manually switched off in those cases. (I think this should already work, but it needs testing.)
- When the final value is available, make sure that it is converted as part of the normal propagation. (Likewise, this should work.)
- Otherwise, if the model type supports client-side value generation (i.e. the value generator does not return temporary values), then use it.
- Otherwise, value generation should not be configured by default.
Taking the cases from above:
- GUID to string
- String has no value generation, so store-type generation cannot be used.
- Client-side value generator is available for GUID, so it is used.
- Int to string
- String has no value generation, so store-type won't be used
- Int does not support client-side generation, so it won't be used
- No value generation for this case
- String to GUID
- Generation is available for GUID, so it is used.
This also covers the common cases where a store type is being wrapped in, for example, a struct. Since the store-type allows value generation, it will be used.
Removing this from the backlog to allow others on the team to review this proposal.
@ajcvickers Sorry, I possibly read the discussion not accurately enough and repeated what was already said.
@ilmax Team agrees that this is a reasonable starting point.