Predicate Expression out of sync

Posts   
 
    
Posts: 54
Joined: 22-Jun-2010
# Posted on: 03-Sep-2010 10:46:59   

Hi

I have written a very small function like below

var bank = new AccountnatureEntity(); filteraccountnature.Clear(); var filterdescription = new FieldCompareValuePredicate(AccountnatureFields.Description, null, ComparisonOperator.Equal, ChangeCase.ToUpperCase(description)); var filterflag = new FieldCompareValuePredicate(AccountnatureFields.Flag, null, ComparisonOperator.Equal, StandardFlag.recordvalidflag); var filteraccountnatureid = new FieldCompareValuePredicate(AccountnatureFields.AccountnatureId, null, ComparisonOperator.NotEqual, accountnatureid); filterdescription.CaseSensitiveCollation = true; filteraccountnature.Add(filterdescription); filteraccountnature.Add(filterflag); filteraccountnature.Add(filteraccountnatureid); adapteraccountnature.FetchEntityUsingUniqueConstraint(accountnature, filteraccountnature); return (accountnature.Fields.State) == EntityState.Fetched;

It works fine. when a keep a break on adapteraccountnature.FetchEntityUsingUniqueConstraint(accountnature, I can see an error for each field threw an error base {SD.LLBLGen.Pro.ORMSupportClasses.ORMException} = {"The entity is out of sync with its data in the database. Refetch this entity before using this in-memory instance."}. But surprisingly it works fine inspite of error !

I dont understand what am diong wrong here

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Sep-2010 11:17:10   

Which runtime library version are you using? Why are you using "FetchEntityUsingUniqueConstraint()" while you are not using any UC (i.e. customer.ConstructFilterForUCCompanyName)

Are you fetching bank or accountnature? Coz I can't see where accountnature has come from?

Posts: 54
Joined: 22-Jun-2010
# Posted on: 03-Sep-2010 11:19:40   

Walaa wrote:

Which runtime library version are you using? Why are you using "FetchEntityUsingUniqueConstraint()" while you are not using any UC (i.e. customer.ConstructFilterForUCCompanyName)

Are you fetching bank or accountnature? Coz I can't see where accountnature has come from?

This line var bank should be read as var accoutnature. Thought of using FetchEntityUsingUniqueConstraint since it is serving my purpose. I always use the latest runtime. I am fetching acccount nature only

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Sep-2010 11:25:13   

Perhaps the real code is different than the posted code, and in this case it might be possible that acountNature has been used and saved at some other place in code before being fetched. In this case it will give an OutOfSyncException when you try to access any of it's fields, since it hasn't been fetched since it was saved to the database, and that's what you do when you access the fields in the debugger.

Posts: 54
Joined: 22-Jun-2010
# Posted on: 03-Sep-2010 12:18:03   

yogesh_shtty wrote:

Walaa wrote:

Perhaps the real code is different than the posted code, and in this case it might be possible that acountNature has been used and saved at some other place in code before being fetched. In this case it will give an OutOfSyncException when you try to access any of it's fields, since it hasn't been fetched since it was saved to the database, and that's what you do when you access the fields in the debugger.

Dear Walaa, I have attachd the code snippet. I still dont think i have written anything wong. but you are the best judge to say whether i have done something wrong or not.

In load data I dont get out of sync but in Isduplicaterecordupdate i get out of sync

In upate click before calling update function am just calling Isduplicaterecordupdate

bool duplicaterecordupdate = Isduplicaterecordupdate( tbxdescription.Text.Trim(), Convert.ToInt32( GlobalVariable.searchaccountnatureid));

Attachments
Filename File size Added on Approval
shttyCode.txt 4,360 03-Sep-2010 13:32.21 Approved
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Sep-2010 00:44:41   

Hi,

I don't understand 100% your code. For instance, I see the Isduplicaterecordupdate method but I don't see where you are calling it.

Also, I don't see the DoRefresh method definition. As in Updatedata you are saving the new entity without refetching and then you call DoRefersh.

BTW, you only receives the exception if you inspect the object in a break debugging. So it could be that you are saving an entity without refetching and without accessing properties further in the code, but when you try to access such properties, debugger throws exception because the entity is out of sync (values saved but not refetched back).

David Elizondo | LLBLGen Support Team
Posts: 54
Joined: 22-Jun-2010
# Posted on: 06-Sep-2010 05:57:54   

daelmo wrote:

Hi,

I don't understand 100% your code. For instance, I see the Isduplicaterecordupdate method but I don't see where you are calling it.

Also, I don't see the DoRefresh method definition. As in Updatedata you are saving the new entity without refetching and then you call DoRefersh.

BTW, you only receives the exception if you inspect the object in a break debugging. So it could be that you are saving an entity without refetching and without accessing properties further in the code, but when you try to access such properties, debugger throws exception because the entity is out of sync (values saved but not refetched back).

Well, now I got the exact problem but still no solution. Let us look at this simple code for another table what I have written

private static bool Isduplicaterecordinsert(string description) { try {

            var country = new CountryEntity();
            filtercountry.Clear();
            var filterdescription = new FieldCompareValuePredicate(CountryFields.Description,
                                                                   null,
                                                                   ComparisonOperator.Equal,
                                                                   ChangeCase.ToUpperCase(
                                                                       description));

            var filterflag = new FieldCompareValuePredicate(CountryFields.Flag, null,
                                                            ComparisonOperator.Equal,
                                                            StandardFlag.recordvalidflag);
            filterdescription.CaseSensitiveCollation = true;
            filtercountry.Add(filterdescription);
            filtercountry.Add(filterflag);


            adaptercountry.FetchEntityUsingUniqueConstraint(country, filtercountry);
            return (country.Fields.State) == EntityState.Fetched;
        }
        catch (Exception ex)
        {
            GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
            return true;
        }
        finally
        {
            adaptercountry.CloseConnection();
        }
    }

I am passing description to the function. Description is being passed directly from texbox on the form.

Problem: Now say I pass UK as description and UK exists in database. It does not give out of sync error but each filter gives error as mentioned below. It gives out of sync only if UK does not exist in DB. This is the actual problem.

If I keep break line by line, for each filter defined I get an error "This object was constructed using a non-selfservicing constructor. Can't retrieve an IEntityField after that." and then finally out of sync in line adaptercountry.FetchEntityUsingUniqueConstraint(country, filtercountry);

Stack trace is

at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.get_Field() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\QueryApiElements\FieldCompareValuePredicate.cs:line 464

I am using Oracle 9i/10g

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Sep-2010 09:15:25   

Is adaptercountry an instane of DataAccessAdapter?

Posts: 54
Joined: 22-Jun-2010
# Posted on: 06-Sep-2010 09:20:27   

Walaa wrote:

Is adaptercountry an instane of DataAccessAdapter?

Yes walaa

Declared in form at one place as follows

private static readonly DataAccessAdapter adaptercountry = new DataAccessAdapter(); private static readonly IPredicateExpression filtercountry = new PredicateExpression();

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 06-Sep-2010 13:18:13   

yogesh_shtty wrote:

Hi

I have written a very small function like below

var bank = new AccountnatureEntity(); filteraccountnature.Clear(); var filterdescription = new FieldCompareValuePredicate(AccountnatureFields.Description, null, ComparisonOperator.Equal, ChangeCase.ToUpperCase(description)); var filterflag = new FieldCompareValuePredicate(AccountnatureFields.Flag, null, ComparisonOperator.Equal, StandardFlag.recordvalidflag); var filteraccountnatureid = new FieldCompareValuePredicate(AccountnatureFields.AccountnatureId, null, ComparisonOperator.NotEqual, accountnatureid); filterdescription.CaseSensitiveCollation = true; filteraccountnature.Add(filterdescription); filteraccountnature.Add(filterflag); filteraccountnature.Add(filteraccountnatureid); adapteraccountnature.FetchEntityUsingUniqueConstraint(accountnature, filteraccountnature); return (accountnature.Fields.State) == EntityState.Fetched;

It works fine. when a keep a break on adapteraccountnature.FetchEntityUsingUniqueConstraint(accountnature, I can see an error for each field threw an error base {SD.LLBLGen.Pro.ORMSupportClasses.ORMException} = {"The entity is out of sync with its data in the database. Refetch this entity before using this in-memory instance."}. But surprisingly it works fine inspite of error !

I dont understand what am diong wrong here

The errors you see in the debugger are there because the debugger reads the values from the properties, which gives an error because you have saved the entity and it hasn't been refetched yet.

yogesh_shtty wrote:

Well, now I got the exact problem but still no solution. Let us look at this simple code for another table what I have written

private static bool Isduplicaterecordinsert(string description) { try {

            var country = new CountryEntity();
            filtercountry.Clear();
            var filterdescription = new FieldCompareValuePredicate(CountryFields.Description,
                                                                   null,
                                                                   ComparisonOperator.Equal,
                                                                   ChangeCase.ToUpperCase(
                                                                       description));

            var filterflag = new FieldCompareValuePredicate(CountryFields.Flag, null,
                                                            ComparisonOperator.Equal,
                                                            StandardFlag.recordvalidflag);
            filterdescription.CaseSensitiveCollation = true;
            filtercountry.Add(filterdescription);
            filtercountry.Add(filterflag);


            adaptercountry.FetchEntityUsingUniqueConstraint(country, filtercountry);
            return (country.Fields.State) == EntityState.Fetched;
        }
        catch (Exception ex)
        {
            GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
            return true;
        }
        finally
        {
            adaptercountry.CloseConnection();
        }
    }

I am passing description to the function. Description is being passed directly from texbox on the form.

Problem: Now say I pass UK as description and UK exists in database. It does not give out of sync error but each filter gives error as mentioned below. It gives out of sync only if UK does not exist in DB. This is the actual problem.

WHERE in the above code do you get the error? You post a 'stacktrace' but that's not complete, you should post the entire stacktrace so the line in the code above is shown.

If I keep break line by line, for each filter defined I get an error "This object was constructed using a non-selfservicing constructor. Can't retrieve an IEntityField after that."

WHERE do you get this error: in the debugger? / watch? Or as an exception?

and then finally out of sync in line adaptercountry.FetchEntityUsingUniqueConstraint(country, filtercountry);

Stack trace is

at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.get_Field() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\QueryApiElements\FieldCompareValuePredicate.cs:line 464

I am using Oracle 9i/10g

Not a complete stacktrace, please post the complete stacktrace.

yogesh_shtty wrote:

Walaa wrote:

Is adaptercountry an instane of DataAccessAdapter?

Yes walaa

Declared in form at one place as follows

private static readonly DataAccessAdapter adaptercountry = new DataAccessAdapter(); private static readonly IPredicateExpression filtercountry = new PredicateExpression();

Please don't declare things statically, you will run into threading issues if this is a webapplication. ALWAYS create a new adapter and predicate, it's very cheap (no performance loss) and you wont run into problems.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 54
Joined: 22-Jun-2010
# Posted on: 06-Sep-2010 13:59:22   

I get this error when I keep a break point in var description and when I scroll down and fields i get error "This object was constructed using a non-selfservicing constructor. Can't retrieve an IEntityField after that."

then under stack trace i get" at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.get_Field() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\QueryApiElements\FieldCompareValuePredicate.cs:line 464"

error is in debugger not in exception

It is winform application and not web app. Still you suggest not to declare things statically ?

While saving, I am refteching the entity after save bool succeeded = adaptercountry.SaveEntity(country,true);

Details attached

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Sep-2010 16:04:40   

The error in the screenshot is logical: the 'Field' property is selfservicing specific, you should check the 'FieldCore' property.

Posts: 54
Joined: 22-Jun-2010
# Posted on: 06-Sep-2010 16:09:49   

Walaa wrote:

The error in the screenshot is logical: the 'Field' property is selfservicing specific, you should check the 'FieldCore' property.

Dont see any error there how ever attached screen shot. But my outofsync remains though am fetching entity afer save

Posts: 54
Joined: 22-Jun-2010
# Posted on: 06-Sep-2010 16:19:58   

I have also attached out of sync error image which shows only the folllowing line in stack trace

at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.GetValue(Int32 fieldIndex, Boolean returnDefaultIfNull) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\EntityBase2.cs:line 2501 at ClubCentricHelper.EntityClasses.CountryEntity.get_Modifieddate() in D:\ProjectLive\LLBLGen Pro Projects\DatabaseGeneric\EntityClasses\CountryEntity.cs:line 702

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Sep-2010 04:09:59   

The logical explanation is that no rows in your DB match your filter expression. If no rows are returned, the entity is marked as OutOfSync. So maybe you should check the entityFields.State or the entity.IsNew property to know if the entity was already fetched and you can read values. The same applies to debugging. If you try to access properties of a non-existent entity (fetched but not existent = out of sync) you will get that error.

David Elizondo | LLBLGen Support Team
Posts: 54
Joined: 22-Jun-2010
# Posted on: 07-Sep-2010 06:01:21   

daelmo wrote:

The logical explanation is that no rows in your DB match your filter expression. If no rows are returned, the entity is marked as OutOfSync. So maybe you should check the entityFields.State or the entity.IsNew property to know if the entity was already fetched and you can read values. The same applies to debugging. If you try to access properties of a non-existent entity (fetched but not existent = out of sync) you will get that error.

Hi Daemo. Exactly. I am trying to see whether value exists in DB or not. I am using **return (accountnature.Fields.State) == EntityState.Fetched; **as mentioned by you.

If this is how it works I mean out of sync is what it gives and still it works ! then am ok wid it