This is a migrated thread and some comments may be shown as answers.

How to return the newly created scope identity

3 Answers 53 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
eraashishgupta
Top achievements
Rank 1
eraashishgupta asked on 08 Jun 2010, 01:19 PM
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,



3 Answers, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 09 Jun 2010, 10:13 AM
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.
0
eraashishgupta
Top achievements
Rank 1
answered on 10 Jun 2010, 08:36 PM
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.

0
Damyan Bogoev
Telerik team
answered on 14 Jun 2010, 05:24 PM
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.
Tags
Development (API, general questions)
Asked by
eraashishgupta
Top achievements
Rank 1
Answers by
Serge
Telerik team
eraashishgupta
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or