override insert

2 posts, 1 answers
  1. Trevor
    Trevor avatar
    13 posts
    Member since:
    Jun 2011

    Posted 15 Nov 2012 Link to this post

    I need to create a sequential number for an identifier, but that sequential number is based on a group code(GGG-100,GGG-101,GGG-102,etc...).  I was going to do this just before the entity was committed to the database on the insert method by finding the highest number based on the group code and adding one. However, the insert method for the service is as follows.
    Public Sub InsertChangeOrders(ByVal _changeOrder As ChangeOrder)
            'This is a callback method. The actual Insert is performed internally.
        End Sub
    Is there some way to override the insert method to perform the lookup and create the number just before committing it to the database?
  2. Answer
    Alexander avatar
    727 posts

    Posted 20 Nov 2012 Link to this post

    Hi Trevor,

    Unfortunately you will not be able to do that in the service code. However, you could use the IInstanceCallbacks interface that OpenAccess provides and implement it on the classes for which you need to update the id manually.

    To implement the interface, just create a partial class for your domain class in a new file (in order not to lose the changes if the autogenerated file is rewritten) and implement the three methods of the interface there:
    Imports Telerik.OpenAccess
    Partial Public Class ChangeOrder
        Implements IInstanceCallbacks
        Public Sub PostLoad() Implements IInstanceCallbacks.PostLoad
            'do nothing
        End Sub
        Public Sub PreRemove(objectScope As IObjectScope) Implements IInstanceCallbacks.PreRemove
            'do nothing
        End Sub
        Public Sub PreStore() Implements IInstanceCallbacks.PreStore
            'your code goes here
        End Sub
    End Class

    In this case you need to implement only the PreStore method, which is called during SaveChanges, right before the record is inserted to the database.
    More information about this interface is available here. Hope that helps.

    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
Back to Top