dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

Make annotations available in TypeMappingInfo #14000

Open roji opened 5 years ago

roji commented 5 years ago

When constructed from an IProperty, TypeMappingInfo currently extracts various information from it (e.g. ClrType), but does not keep the IProperty itself. This makes it impossible for the type mapper to access any annotations applied to the property which could influence the mapping.

For a concrete example, PostgreSQL PostGIS supports the following store type: GEOMETRY (POINT,4326). This acts as a constraint on the column to only accept points with SRID 4326. Now, we can add ForNpgsqlHasSrid() extension method just like Sqlite, but we have no way to flow this information into Npgsql's relational type mapping, to make it construct the right store type string.

As a workaround, I can catch this annotation in my MigrationsSqlGenerator, take the partial store type given by the mapping, and patch it to contain the SRID. However, this really seems like a hack (not to mention that spatial support is implemented in a plugin and MigrationsSqlGenerator is part of the provider). If the mapping could access the annotation, it would be able to generate a complete store type and this wouldn't be needed.

/cc @austindrenski

ajcvickers commented 5 years ago

@roji The general problem here is that we need to be able to build the same type mapping from the model snapshot as was built originally. If we store and expose the IProperty, then providers will use this in the type mapping process--as you are asking for--but then we won't be able to reliably re-create it from the model snapshot, resulting in a different type mapping and a bogus migration. So it's possible that we need to make the property annotations available and at the same time make sure those annotations make it into the snapshot. /cc @AndriySvyryd @bricelam

AndriySvyryd commented 5 years ago

All non-excluded annotations should be in the snapshot already, so we would only need to add them to the TypeMappingInfo