Entity Updates and Expressions

Posts   
 
    
Posts: 20
Joined: 27-Jun-2011
# Posted on: 25-Aug-2011 03:40:20   

I'm trying to run an update on the database using Entity Updates. However I'm getting a stack overflow. What I want to do is simple add 10 to a value in all rows of a table. My code is

        entity = new UsermasEntity();
        e = (EntityField2)entity.Fields[NameConvert.ConvertFieldName(f.FieldName)];
        e.ExpressionToApply = new Expression(e, ExOp.Add, 10);

        da.UpdateEntitiesDirectly(entity, null);
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Aug-2011 04:23:41   

Please post the full exception message and stack trace. And your LLBLGen runtime library version as well. (http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7722)

David Elizondo | LLBLGen Support Team
Posts: 20
Joined: 27-Jun-2011
# Posted on: 25-Aug-2011 05:42:52   

daelmo wrote:

Please post the full exception message and stack trace. And your LLBLGen runtime library version as well. (http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7722)

LLBLGEN v3.1 Final May 13 2011 The runtime is 3.1.11.0518

You cant trap a system.stackoverflowexception. VS completely locks up and I have to terminate the app being debugged with Task manager. VS then comes back with System.StackOverflowException but the program has terminated.

Posts: 20
Joined: 27-Jun-2011
# Posted on: 25-Aug-2011 05:53:33   

daelmo wrote:

Please post the full exception message and stack trace. And your LLBLGen runtime library version as well. (http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7722)

Maybe i should ask the question

How do i write the expression to get the result

update table set table_field1 = table_field1 + 10

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Aug-2011 10:52:27   

Please check the code example, found in Expressions in entity updates

Posts: 20
Joined: 27-Jun-2011
# Posted on: 26-Aug-2011 02:27:12   

Walaa wrote:

Please check the code example, found in Expressions in entity updates

I have checked that out. My code example is what I posted above, but im getting a stack overflow. Is what Im doing at fault?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Aug-2011 06:22:42   

I didn't notice it before but I see now what the problem is...

entity = new UsermasEntity();
e = (EntityField2)entity.Fields[NameConvert.ConvertFieldName(f.FieldName)];
e.ExpressionToApply = new Expression(e, ExOp.Add, 10);

da.UpdateEntitiesDirectly(entity, null);

'e' is an instance of EntityField2. At the same time you are using such instance in the expression. So when the expression is expanded, it ends with:

theField = (((theField  (...) + 10) + 10)  + 10)

so the expression is evaluated, then inside the 'e' parameter is evaluated, the expression is evaluated again which evaluates the field, which evaluates the expression .... boom!

So, inside the expression use the [Entity]Fields.TheFild class instead. This will create a new instance of the field to be used. So, in normal circunstances you should write the code like:

var entity = new OrderEntity();         
entity.Fields["Freight"].ExpressionToApply = new Expression(OrderFields.Freight, ExOp.Add, 10);

new DataAccessAdapter().UpdateEntitiesDirectly(entity, null);

However, I see you are using a generic way of passing the field, as if you didn't know the field you will use in the expression in advance. So, try this modification in your code:

entity = new UsermasEntity();
entity.Fields[NameConvert.ConvertFieldName(f.FieldName)].ExpressionToApply =
    new Expression((EntityField2)EntityFieldFactory.Create((UsermasFieldIndex)f.FieldIndex), ExOp.Add, 10);

da.UpdateEntitiesDirectly(entity, null);
David Elizondo | LLBLGen Support Team