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:
Hi,
If you received notification on this ticket and clicked later, you might have found nothing because I removed my initial comment.
If you were unlucky to see it, then you can guess why I removed it.
I ran into this ticket with fire under my feet and running out of options to solve issue caused by random guids in clustered index used by BaseObject by default.
I lost my not-so-fine-temper and went with rather offensive and completely non-constructive comment. In the mean time I decided to go another route, so I simply removed the comment.
So I do apologize both to customers and DevExpress about this, will try to do better. Hopefully :-)
On the subject matter I do think XPO should support read-only persistent properties out of the box and using attributes seems like most convenient approach.
Creating UnitOfWork descendants can be a problem if you have a lot of old code (like me) from time when UnitOfWork was still recommended way of executing direct SQL queries and especially doing work on batch of data in code.
That recommendation later changed to IObjectSpace, but thousands of lines of code is not easy to change especially when it is working. Also, IObjectSpace still cant execute queries directly, so going directly to UnitOfWork is still required in many cases - but this is another topic - just pointing why there is loads of UnitOfWork code in my case.
Regards,
Mario
+1 for Mario comment - please supply the feature out of the box
+1 for Mario
It's amazing that after 7 years (and an update after 5 years) this feature has not found it's way into the product especially the code for its implementation has been provided by the community :(
I developed on an application the last five years and it has thousands lines of code now.
For the next update, I need a new calculated field on the database and have to use it in the application.
It makes no sense, when I implement an own session implementation which will affect my whole application just because of one calculated field…
You know, I think you guys do an amazing work with your framework and your support is really great!
But I have to fully agree with Olivier Gröger: I can't believe, you didn't implement this. You have the code…
Is there still no out of the box solution in v18.1.4 or is it planned in one of the next updates?
same with us. have application for 6 years and now we are stuck!
really wonder why it could not be implemented?
you should move forward with database enhancements, that enhance performance etc.
@Mysoft Systems and others: This functionality is already implemented in v18.2:-)
We are about to roll out the blog post for early testers. Please track our XPO team blog, as always: https://community.devexpress.com/blogs/xpo/default.aspx
Amazing
Cant wait!
this was hard to find in 2020…
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.