Property defining expressions for query: mapping model properties to non-column expressions
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 42/100
Research direction
Start with the FromSql query path and the existing NotMappedAttribute behavior described in the issue, then trace how model properties are mapped to result-set columns and schema migrations. Define how a dynamically mapped property should behave when its calculated result column is present or absent, without creating or storing a schema column. Done means the behavior is specified and covered for querying, migrations, and saving.
Written by the indexing model from the issue text.
Description
Certain search strategies (full text search, spatial search, ...) leverage underlying database tech for better indexing and retrieval.
Scenario: I have "latitude" and "longitude" columns in my schema. I would like to map a property "Distance" to a column in the result set that evaluates the distance between those coordinates and user coordinates, i.e. SQL like:
SELECT
[latitude],
[longitude],
calculate_distance(latitude, longitude, @user_latitude, @user_longitude) AS [Distance]
FROM coordinates;
The easiest way to do this is to add a Distance to the model as an always NULL column, and then use context.Coordinates.FromSql("...", sqlParametes) when querying. The downside is that this creates an extraneous column in the schema. (I was hoping I could use NotMappedAttribute but that obviously also suppresses value mapping when querying).
As a workaround, I have to do the following steps:
- Manually drop the columns in a migration script
- Set
Property("Distance").Metadata.BeforeSaveBehaviourandProperty("Distance").Metadata.BeforeSaveBehaviourtoPropertySaveBehaviour.Ignore - Ensure that all queries return a result set with a calculated
[Distance]column, to satisfy EF's assumption that there is one.
A better approach would be to have something like [DynamicallyMappedAttribute] - similar to [NotMappedAttribute], it suppresses column creation and value storage, but has a different value retrieval behaviour - i.e. when querying I would like for EF to tolerate it these properties are not present as a column in the result set. Map them when they are there and if not, just set them to NULL.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100