Support saving no-setter properties (for example expression bodied properties)

Open
#2,319 10 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
databases

Research direction

Start with the Blip example and review EF Core's property mapping, materialization, change tracking, and save paths to determine where no-setter properties are currently excluded. Compare the requested behavior with the existing discussion and define tests that show expression-bodied properties can be saved without adding a setter, while remaining excluded from materialization when appropriate.

Written by the indexing model from the issue text.

Description

area-o/c-mapping

Note: updated to be a clear feature request based on thread discussion. Thanks!

There are good use cases for storing get-only properties (e.g., for BI tools, JSON based data feeds, and other non-EF consumers), even when those have no role in materialization.
Consider this fact oriented entity class describing a signal from a sensor:

public class Blip 
{
    public int OldState {get; set;}
    public int NewState{get;set;}
    // implying enum here but could be any EF compatible type
    public SensorAction SensorActionDetected=> Domain logic expression which resolves meaningful SensorActionDetected from raw sensor values OldState and NewState
}

After much experimentation the only reliable way I've been able to map such properties (in beta 4) is to lie and create a false setter so EF thinks it can materialize this property, such as:

    public SensorAction SensorActionDetected{ get{ return logic here} set{ ignore value } }

This implementation feels like really dirtying the domain class for the sake of EF compatibility. Future users of my data model would likely be baffled but the no-op setter.
Another option would be to make SensorActionDetected a simple auto-property without a custom getter, and set the value elsewhere, like in the set method of NewState, but this falls apart as the number of properties used in a calculation grows, you have to special case every other property setter if you can't control what order they will be populated in.
Another option would be to not include SensorActionDetected in the class and instead calculate it elsewhere such as inside SQL Server, but then you need parallel domain expert implementations of the SensorActionDetected logic everywhere Blip goes (SQL, JSON, BI Tools, etc. etc.) which is not desirable.

Please support the ability to save no-setter properties. Thanks.

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.