efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Support for custom type mapping and data store to CLR type conversions

Open rowanmiller opened this issue 12 years ago • 66 comments

There is a continuum of scenarios that can be supported here:

  • [x] Allow for simple hard coded conversions between types that are related in a well-know manner to types that are supported. E.g.:
    • char can map to the database exactly as a string of size 1 (see #8656)
    • byte can map to the database exactly the same as a byte[]
    • signed or unsigned small integers can fit in the nearest wider signed or unsigned integer
  • [x] 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
  • [x] 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 #10434 and #10861
  • [x] 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:

  • [x] Parameters
  • [x] Expressions for value access in materialization
  • [ ] Expressions for equality and inequality comparisons - see #10265
    1. 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 #10265
    1. Comparisons and sorting are equivalent as long as the conversion is a monotonic function (although reversed if it is decreasing)
    2. 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 #10434

rowanmiller avatar May 22 '14 20:05 rowanmiller

Just checking - Is this the work item tracking this uservoice item? Thanks.

mattjohnsonpint avatar Nov 07 '14 06:11 mattjohnsonpint

Any movement on this? Or hint of direction, proposed API, anything? Thanks.

mattjohnsonpint avatar Jul 12 '15 18:07 mattjohnsonpint

@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.

rowanmiller avatar Jul 15 '15 17:07 rowanmiller

Thanks!

mattjohnsonpint avatar Jul 15 '15 18:07 mattjohnsonpint

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

smitpatel avatar Sep 04 '15 19:09 smitpatel

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?

ilmax avatar Oct 08 '15 15:10 ilmax

This is exactly what I was recently looking to do. It would be an extremely useful to have this.

Rseding91 avatar Oct 27 '15 05:10 Rseding91

I need the same possibility as mentioned by @ilmax:

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?

Does this allow me to do that?

Marusyk avatar Nov 21 '15 00:11 Marusyk

Please provides interfaces can add to model binder or use as attributes for converting any types. for example convert string to xml or json object, custom calendar datetime or any class or struct and inverse any types to string for save to database, so can map saved data to any database types.

mojtabakaviani avatar Jan 04 '16 08:01 mojtabakaviani

@rowanmiller - it would be really helpful if you could please give us a status update or at least the general design plan for this feature. Thanks.

mattjohnsonpint avatar Jan 08 '16 14:01 mattjohnsonpint

@mj1856 this will definitely be a post-7.0.0 feature. We haven't done any planning/prioritization of work items for post-7.0.0 yet. We'll be doing it around the time of the 7.0.0 release and will share plans as soon as we have them.

rowanmiller avatar Jan 08 '16 17:01 rowanmiller

Thanks.

mattjohnsonpint avatar Jan 11 '16 03:01 mattjohnsonpint

do you have any plan to implement this feature/api ?

netcore-jroger avatar Jun 28 '16 04:06 netcore-jroger

No exact plans of release etc. It is lower priority that some other features, but we may end up implementing it as part of one of the higher priority features as it may help with the implementation.

rowanmiller avatar Jun 28 '16 16:06 rowanmiller

This is third highest ranked feature request on uservoice I was looking for such ability in EF 6.1.3, but without luck. Having this build-in would be very useful. Version 1.0.0 is already released, maybe now this can be implemented. @rowanmiller any news?

Misiu avatar Aug 09 '16 06:08 Misiu

@Misiu this is one of three features we will start working on soon although they are not really planned to ship as part of EF Core 1.1. See https://blogs.msdn.microsoft.com/dotnet/2016/07/29/entity-framework-core-1-1-plans/ for more details on 1.1 priorities.

divega avatar Aug 09 '16 07:08 divega

@divega thanks for link. Hopefully everything will go well and simple type conversions will be included :smile:

Misiu avatar Aug 09 '16 07:08 Misiu

Enum to a db lookup table please!

dirq avatar Aug 10 '16 14:08 dirq

As a proof of concept, please consider a project that uses an entity such as:

public class Person
{
    public int Id {get; set;}
    public string Name {get; set;}
    public Date BirthDate {get; set;}
}

In this case, the BirthDate property is a System.Date struct, found in the System.Time nuget package from corefxlab.

The entity should be able to participate in all EF normal CRUD operations, including querying with the field in a filter, sort, or projection. The backing store for the field should be a SQL date type.

The developer should be able to somehow write a fluent mapping or class with to/from methods that control how the custom data type hooks in to EF. They should not have to think about it on a per-operation basis.

mattjohnsonpint avatar Aug 10 '16 17:08 mattjohnsonpint

Just to provide another use case for this. In our system we have batch numbers, these are a string but that string has a great deal of meaning so we would like to encapsulate it to prevent unauthorised adaptation and isolate logical operations that are performed on it. If we had a class like:

public class BatchNumber{
   public bool IsReturn();
   public bool IsDispense();
   ...
}

We would be looking for some way to map the VARCHAR database field to this class in the model.

Not sure how this would work from EF's perspective, constructing the object would be easy enough as the author could provide a parameterized constructor, not sure how you'd get the content back out again though as this would obviously not serialize well, a public getter probably wouldn't be desirable either as the purpose of the above is to encapsulate the value, an intention that would be defeated by making the internal value accessible.

I suspect @mj1856 suggestion of fluent mapping with defined to/from methods would be the simplest and most flexible solution.

AlanParr avatar Nov 23 '16 22:11 AlanParr