updating an object problem

Thread is closed for posting
5 posts, 0 answers
  1. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 29 Sep 2009 Link to this post

    Hi,

    I have a question about updating a persistent object from itself. Let say I have a simple class "User" and I create an object retrieving it by his id like this:
    var result =
                    from c in scope.Extent<User>()
                    where c.Id == id_user
                    select c;

              User my_user = result.First();

    Then I want to have a function "do_login()" in which I want to update the "date_last_login" column of that user. The problem here is I want to have that function inside the User class (so I'm calling it my_user.do_login()), but then I get an error saying: "active transaction required for write operation", when I try to change the value of date_last_login. I believe that the exception is thrown because I am creating my object elsewhere and then after it's created I want to change something using a different scope.
    Is there any way it could work like this? Is it possible to retrieve objects and then change them afterwards and commit a transaction without having to use the same scope and doing it in the same function? I want to be able to use an object and change it many times afterwards without knowing when to stop using a scope (and disposing it). 
    I am having problems understanding the concept of "scope", so maybe someone can show me better example of this? Thanks in advance
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 30 Sep 2009 Link to this post

    Hi Miran Zgec,

    In order to set up automatic update in ‘date_last_login’ column, follow these steps:
    1.    During Forward Mapping process select 'date_last_login' field from User class;
    2.    Check Set field to current date during create and Set field to current date during update;

    In source code where 'data_last_login' is expected to be updated you can do the following:

    IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
    int userId = 1;
    using (scope)
    {
        User user = scope.Extent<User>().Where<User>(u => u.Id == userId).First<User>();
        scope.Transaction.Begin();
        scope.MakeDirty(user, "data_last_login");
        scope.Transaction.Commit();
    }

    Best wishes,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 02 Oct 2009 Link to this post

    Hi Damyan,

    thank you for your answer, but it doesn't really solve my problem. I used the date_last_login field just for a simple example, but I would like to know how to change field values on a previously created (retrieved) object in general. It's just that we were used to create objects in one place and then change their fields values later sometime, which here becomes rather difficult because of the whole scope concept. 
    To make it simple: I would like to have an object that I created (for example in Page_load) and that I could simply update something without having to retrieve the whole thing again from the database (for example make a change on it after clicking a button (postback)).

    Regards,
    Miran
  4. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 05 Oct 2009 Link to this post

    Hi Miran Zgec,

    To achieve this without having to retrieve the object from database you can use the following method inside the User class:

    public void Update(string name, … ) 
        IObjectScope scope = Database.GetContext(thisas IObjectScope; 
        if (!scope.Transaction.IsActive) 
        { 
            scope.Transaction.Begin(); 
        } 
        this.Name = name; 
        …… 
        scope.Transaction.Commit(); 

    The Database.GetContext(object) method determines the object scope or container instance managing the passed object.
    Note that if you have already disposed the scope that the object belongs to, you will get an exception. In order to avoid it, you can use the scope-per-session approach and keep the object scope in the page context. Then, when you need the scope, you can get it from the page context instead of using the Database.GetContext() method.

    I hope that will help you.

    Greetings,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Miran Zgec
    Miran Zgec avatar
    14 posts
    Member since:
    Oct 2006

    Posted 08 Oct 2009 Link to this post

    Thank you Damyan, your last answer solves my problem (for now), Database.GetContext(this) is what I have been looking for.

    Regards, 
    Miran
Back to Top