Ticket S39006
Visible to All Users

How to map a property to a read-only database column

created 13 years ago (modified 5 years ago)

Scenario
There are situations where column values need to be retrieved from the database but cannot be written to it. Here are some typical scenarios when you need to map XPO class properties to:
1. Computed database columns: when it is inefficient to implement them as read-only using PersistentAliasAttribute.
2. Auto-increment database columns that are not primary keys (for example, rowversion).

Current Solutions (v18.2+)

C#
public class MyClass : XPLiteObject { // ... [FetchOnly] [Persistent("Total")] private double total; [PersistentAlias("total")] public double Total { get { return total; } } }

Applied to a class property or field, FetchOnlyAttribute indicates that you can only read values from a database. XPO will not include these values in INSERT  and UPDATE SQL statements. For more information, see Easy Way to Map a Property to a Read-Only Database Column.

Old Solutions (older than v18.2)
To implement this requirement with XPO, you can create a custom UnitOfWork or Session descendant and override the GetPropertiesListForUpdateInsert method. See examples here:

A custom UnitOfWork can be used in XAF by implementing a custom object space and object space provider:

Show previous comments (7)
MS MS
Mysoft Systems 7 years ago

    Amazing
    Cant wait!

      this was hard to find in 2020…

      DevExpress Support Team 5 years ago

        Hi,

        Would you please clarify how you tried to find this information? I used the following search string: https://search.devexpress.com/?q=read%20only%20column&f=70&m=SupportCenter. This article is the top most in the search list.

        Answers approved by DevExpress Support

        created 7 years ago (modified 6 years ago)

        We have added support for read-only database columns in XPO in v18.2. Now, you can apply the FetchOnly attribute to a field or property to indicate that you can only read values from a database. XPO will not include these values in INSERT  and UPDATESQL statements.

        C#
        public class MyClass : XPLiteObject { // ... [FetchOnly] [Persistent("Total")] private double total; [PersistentAlias("total")] public double Total { get { return total; } } }

        You can also set the FetchOnly option in the Data Model Designer. In the metadata, the XPMemberInfo.IsFetchOnly  property returns True for such members.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.