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

updating an object problem

4 Answers 89 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Miran Zgec
Top achievements
Rank 2
Miran Zgec asked on 29 Sep 2009, 01:52 PM
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

4 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 30 Sep 2009, 01:28 PM
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.
0
Miran Zgec
Top achievements
Rank 2
answered on 02 Oct 2009, 07:07 AM
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
0
Damyan Bogoev
Telerik team
answered on 05 Oct 2009, 04:31 PM
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.
0
Miran Zgec
Top achievements
Rank 2
answered on 08 Oct 2009, 10:47 AM
Thank you Damyan, your last answer solves my problem (for now), Database.GetContext(this) is what I have been looking for.

Regards, 
Miran
Tags
General Discussions
Asked by
Miran Zgec
Top achievements
Rank 2
Answers by
Damyan Bogoev
Telerik team
Miran Zgec
Top achievements
Rank 2
Share this question
or