How to return the newly created scope identity

4 posts, 0 answers
  1. eraashishgupta
    eraashishgupta avatar
    19 posts
    Member since:
    Nov 2008

    Posted 08 Jun 2010 Link to this post

    Hi all,
    In the database stored procs we return the newly created scope identity to return the identity element.
    what do we do here with openacess ORM for that case
    like i am doing
     scope.Transaction.Begin(); 
                    scope.Add(item); 
                    scope.Transaction.Commit(); 
    so how to return the latest created identity value back to the client

    Thanks,



  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 09 Jun 2010 Link to this post

    Hello Aashish,

     Telerik OpenAccess ORM actually handles this pretty well. After an insert such as this one the identity fields of this entity are populated with the new values. You just need to access them.

    If you want something generic you can always use the scope.GetObjectId method.

    I do hope this helps.

    Greetings,
    Serge
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. eraashishgupta
    eraashishgupta avatar
    19 posts
    Member since:
    Nov 2008

    Posted 10 Jun 2010 Link to this post

    Hi ,
     
     public T Create(T item) 
            { 
                using (var scope = ObjectScopeProvider1.GetNewObjectScope()) 
                { 
                    scope.Transaction.Begin(); 
                    scope.Add(item); 
                    scope.Transaction.Commit(); 
                    return item; 
                } 
            } 
     

    Here is the full code of what i am doing and after the insert still i am getting the id as 0 can you tell me what exactly i need to catch so that i get the correct value.

  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 14 Jun 2010 Link to this post

    Hello eraashishgupta,

    You could use the following approach to achieve the goal:

    public class CRUD<T> : IDisposable
        where T : class, new()
    {
        private IObjectScope scope;
      
        public CRUD()
        {
            this.scope = TestingScopeProvider.GetNewObjectScope();
        }
      
        public T Create(T item)
        {
            scope.Transaction.Begin();
            scope.Add(item);
            scope.Transaction.Commit();
            return item;
        }
      
        public IObjectId GetObjectId(T item)
        {
            return this.scope.GetObjectId(item);
        }
      
        #region IDisposable Members
      
        public void Dispose()
        {
            this.scope.Dispose();
        }
      
        #endregion
    }

    ...
    using (CRUD<User> crud = new CRUD<User>())
    {
        User user = new User { FirstName = "First5", LastName = "Last5", Birthdate = DateTime.Now };
        User addedUser = crud.Create(user);
        Console.WriteLine(crud.GetObjectId(addedUser));
    }
    ...


    Additional information regarding the IObjectScope.GetObjectId() method can be found here.
    Hope that helps.

    Kind regards,
    Damyan Bogoev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top