Ticket Q371449
Visible to All Users

Auto increment ID

created 12 years ago

I would like to ask another different question, how to create Auto increment ID, and should start from the max column Id.

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hello,
You can derive your persistent object from the built-in XPObject class that provides an auto-incremental integer key. If this solution does not meet your needs, please describe your task in greater detail.
For instance, you can refer to the Session.Evaluate problems ticket to learn about an alternative approach to query the max number in a table. Taking your code into account, the following code worked fine in my tests:

C#
Public Overrides Sub AfterConstruction() MyBase.AfterConstruction() EmpKey &= Convert.ToInt32(Session.Evaluate(Of Employee)(DevExpress.Data.Filtering.CriteriaOperator.Parse("Max(EmpKey)"), nothing)) + 1 End Sub

In addition, I suggest you study the following example: How to get the maximum or minimum object's value from a collection.

    Show previous comments (6)
    Dennis Garavsky (DevExpress) 12 years ago

      Unlike the constructor, which is called for both cases when a persistent object is loaded from the database and is created manually, the AfterConstruction method is called only when a new object is created.
      Here is some useful documentation on this method: http://documentation.devexpress.com/#Xaf/CustomDocument3258
      The OnSaved method is inappropriate here, because the transaction is closed at this time already. You should not generally use this method. Take special note of the OnSaving method instead.

      A A
      Aboud Elzubair Mabloul 12 years ago

        Thanks a lot for all your great support.

        Dennis Garavsky (DevExpress) 12 years ago

          You are always welcome!
          P.S.
          Once your question has been resolved to your satisfaction, mark the corresponding answer as a Solution. Your question status will immediately become Closed.
          http://devexpress.com/Support/Center/FAQ
          =================================
          Join the XAF and XPO Fan Club!
          =================================

          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.