You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support saving no-setter properties (for example expression bodied properties) #2319
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:
publicclassBlip{publicintOldState{get;set;}publicintNewState{get;set;}// implying enum here but could be any EF compatible typepublicSensorActionSensorActionDetected=>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:
publicSensorActionSensorActionDetected{get{returnlogic 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.
Reacted by Michiel DP, Joachim Verschelde and Logan Dam
Note that there are several issues open related to read-only properties (list below). I don't mean to duplicate those, but rather ask what the intended/suggested pattern is here and if this isn't supported today to consider it for the future.
changed the title [-]Clear story/support/advice for read-only properties to be saved to storage but not used in materialization[/-][+]Support saving properties with an expression body (and not setter)[/+]on Jun 12, 2015
@MatthewMWR we aren't intending to support this scenario in the initial release (though we do think it would be good to support). The closest you could get with EF Core (or EF6) is to have a private setter that doesn't do anything.
Generally when we talk about "read-only" we are referring to something that can be set/changed before inserting the record, but once saved to the database it cannot be changed.
changed the title [-]Support saving properties with an expression body (and not setter)[/-][+]Support saving no-setter properties (for example expression bodied properties)[/+]on Jun 16, 2015
Edit with example for clarity.
Would it be reasonable to accomplish this via explicit use of shadow properties in model building... or does this violate some assumptions about shadows? Example:
Note that the workaround shown above (explicitly create and manage shadow properties to represent get-only property values in database) no longer works in 1.0.0. Metadata.IsShadowProperty is now read only.
Does this mean a caller should not try to create shadow properties for this scenario, or just that making a shadow property needs to happen somewhere else?
@rowanmiller@ajcvickers Any news regarding this feature request? This would be a very valueable feature in my opinion.
We could extract some common pieces of logic into expression-boddied members, which even reference each other. And then write linq queries using these.
Your suggestion of having a private setter that doesn't do anything throws the error "Invalid column {xyz}" on these properties (EF 8).
Here's a stripped down example of how I would like to use this feature:
This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 9.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you. This issue currently has no votes.
Activity
MatthewMWR commentedon Jun 3, 2015
Note that there are several issues open related to read-only properties (list below). I don't mean to duplicate those, but rather ask what the intended/suggested pattern is here and if this isn't supported today to consider it for the future.
#1527
#1311
#1526
#2318
[-]Clear story/support/advice for read-only properties to be saved to storage but not used in materialization[/-][+]Support saving properties with an expression body (and not setter)[/+]rowanmiller commentedon Jun 12, 2015
@MatthewMWR we aren't intending to support this scenario in the initial release (though we do think it would be good to support). The closest you could get with EF Core (or EF6) is to have a private setter that doesn't do anything.
Generally when we talk about "read-only" we are referring to something that can be set/changed before inserting the record, but once saved to the database it cannot be changed.
MatthewMWR commentedon Jun 16, 2015
Updated main post title and text for clarity as a future feature request. Thanks.
[-]Support saving properties with an expression body (and not setter)[/-][+]Support saving no-setter properties (for example expression bodied properties)[/+]MatthewMWR commentedon Jul 29, 2015
Edit with example for clarity.
Would it be reasonable to accomplish this via explicit use of shadow properties in model building... or does this violate some assumptions about shadows? Example:
MatthewMWR commentedon Jul 26, 2016
Note that the workaround shown above (explicitly create and manage shadow properties to represent get-only property values in database) no longer works in 1.0.0. Metadata.IsShadowProperty is now read only.
Does this mean a caller should not try to create shadow properties for this scenario, or just that making a shadow property needs to happen somewhere else?
AndriySvyryd commentedon Aug 22, 2019
@ajcvickers Is this fixed now with backing field support and PropertyAccessMode?
ajcvickers commentedon Aug 22, 2019
@AndriySvyryd Not as described here. The expression bodied property is not backed by a field.
MichielDP commentedon Jul 15, 2024
@rowanmiller @ajcvickers Any news regarding this feature request? This would be a very valueable feature in my opinion.
We could extract some common pieces of logic into expression-boddied members, which even reference each other. And then write linq queries using these.
Your suggestion of having a private setter that doesn't do anything throws the error "Invalid column {xyz}" on these properties (EF 8).
Here's a stripped down example of how I would like to use this feature:
With the goal of using any of these expression-boddied members in linq queries, for example:
ajcvickers commentedon Jul 17, 2024
This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 9.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you. This issue currently has no votes.