Repository navigation
Closed
Milestone
Description
There is a continuum of scenarios that can be supported here:
- Allow for simple hard coded conversions between types that are related in a well-know manner to types that are supported. E.g.:
charcan map to the database exactly as astringof size 1 (see Remove SqlServer TypeMapping for CLR type char #8656)bytecan map to the database exactly the same as abyte[]- signed or unsigned small integers can fit in the nearest wider signed or unsigned integer
- Allow providers to supply their own additional type mappings for types they don't support, e.g. if a database engine doesn't have native support for bool, it can decide to use a small integer representation
- Allow specific well known scenarios that are commonly demanded, like mapping enum types to strings
- Allow for conversions to be performed on the server (vs. only on the client) for cases in which there isn't a viable CLR representation for the server type - see Support (custom) member/method translations on value-converted types #10434 and Support server side value conversions #10861
- Allow for user provided conversions
They all probably require extending the reach of the type mapper to be able to participate of the generation of:
- Parameters
- Expressions for value access in materialization
- Expressions for equality and inequality comparisons - see Support client evaluation when store evaluation is not appropriate #10265
- Equality on the server should be equivalent to equality on the client as long as the conversion is deterministic and bijective.
- Order comparisons and sorting - see Support client evaluation when store evaluation is not appropriate #10265
- Comparisons and sorting are equivalent as long as the conversion is a monotonic function (although reversed if it is decreasing)
- For non order-preserving conversions, we may need to differentiate the scenarios in which we introduce sorting just to get the order of two or more results to be deterministic so we can zip them together, vs. explicit ordering requested by user, which we may need to evaluate on the client after the conversion is performed.
- Operator and function translation - see Support (custom) member/method translations on value-converted types #10434
Reacted by Rob Prouse, Joseph Musser, Tomasz, Glen, Søren Nguyen Boisen, Rasmus Lilja, groege, Halid Cisse, Alexis Nowikowski, Robert and 41 more
Activity
mattjohnsonpint commentedon Nov 7, 2014
Just checking - Is this the work item tracking this uservoice item? Thanks.
mattjohnsonpint commentedon Jul 12, 2015
Any movement on this? Or hint of direction, proposed API, anything? Thanks.
rowanmiller commentedon Jul 15, 2015
@mj1856 nothing yet. We know we want to do this (and that has influenced how things are architected internally) but we're currently planning to work on lighting this feature up after our initial RTM of EF7.
mattjohnsonpint commentedon Jul 15, 2015
Thanks!
smitpatel commentedon Sep 4, 2015
Details:
At present we are making changes to our property discovery so that all the types supported by the provider can be mapped as primitive properties. In the addition to provider supported types, user may want to use some other type as a primitive by providing a custom typemapping through which it can be mapped to one of the known types of the provider and subsequently be used as a primitive property.
Also see #2588
[-]Flexible data store type to CLR type conversions[/-][+]Add support for custom typemapping for data store type to CLR type conversions[/+][-]Add support for custom typemapping for data store type to CLR type conversions[/-][+]Add support for custom typemapping for data store type to CLR type conversions (aka custom TypeMapping)[/+][-]Add support for custom typemapping for data store type to CLR type conversions (aka custom TypeMapping)[/-][+]Add support for custom typemapping for data store type to CLR type conversions[/+][-]Add support for custom typemapping for data store type to CLR type conversions[/-][+]Add support for custom type mapping for data store to CLR type conversions[/+]ilmax commentedon Oct 8, 2015
Does this allow me to map a custom type to whatever filed in the storage? e.g. create a custom type for entity.Id (which only wraps an int) instead of using int or map enum to char?
Rseding91 commentedon Oct 27, 2015
This is exactly what I was recently looking to do. It would be an extremely useful to have this.
91 remaining items
tystol commentedon Jan 31, 2018
When this was first created back in 2014 (based on a uservoice suggestion from 2012) the intent was obviously for it to be a feature of legacy EF(ie.1-6). So while it is great that EFCore supports this, where does this leave the vast majority of us with existing apps on EF6 (that can't update until EFCore is at feature parity with EF6 - ie. GROUP BY!)
Is there any plans to backport this to EF6?
ajcvickers commentedon Jan 31, 2018
@tystol This issue is not something that our team is planning to address in the EF6.x code base. This does not mean that we would not consider a community contribution to address this issue. However, the nature of the EF6 code and the EDM type system makes it non-trivial to implement. Being able to do things like this in a reasonable way is one of the reasons for EF Core being a new codebase that is not a front for EDM.
essmd commentedon Apr 5, 2018
As already mentioned from @Marusyk and @ilmax:
I already tried ValueConverters and those are awesome features! But today i tried to use a custom type for Identities (primary and foreign keys). In DDD where you want to express your values with more context - an int (primary key and foreign key) is just an int and its value could be anything.
It would be so much value for EF if it would be possible to consider value conversations for primary keys and foreign keys. Is something like this planned or will this issue resolve parts of this?
ajcvickers commentedon Apr 6, 2018
@essmd To clarify, the only part of this that isn't working for you is that the key cannot be store-generated?
essmd commentedon Apr 7, 2018
@ajcvickers Correct! As i can see here in the code, converters are not allowed.
My expectation from this setup was, that the add migration would create an migration with an
INT IDENTITYcolumn, not throwing an exception.Later when SaveChangesAsync is called, EF executes the
INSERT INTOsql query and get the identity/id back from store (INT) and when it comes to map the INT from store to the property, it will use the configured conversation.I tested again with foreign key too, and its also not possible to map foreign keys with properties using custom types and configured converters: