Entity Out of Sync

Posts   
 
    
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 27-Apr-2006 22:58:23   

LLBLGen: Adapter (March 31, 2006) .NET: 2.0

I'm getting the following:

Exception 'The entity is out of sync with its data in the database. Refetch this entity before using this in-memory instance.' occured in 'SD.LLBLGen.Pro.ORMSupportClasses.NET20' at 4/27/2006 12:56:52 PM

StackTrace
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.GetCurrentFieldValue(Int32 fieldIndex)
   at HLPUSD.SMART.DAL.EntityClasses.StuStatusEntity.get_LocId()
   at HLPUSD.SMART.BLL.StudentManager..ctor(String stuID)
   at HLPUSD.SMART.ParentPortal.StuAttnDetail.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at HLPUSD.SMART.ParentPortal.webFormSuper.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

...in a web app where I don't do any inserts. What could be some reasons to get this error if there is no insert activity? Is there a timeout that occurs?

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 27-Apr-2006 23:13:41   

I've gotten this error trying to access a property of an entity that didn't fetch correctly. I think I had assigned it a primary key that didn't exist and tried to fetch. When the fetch failed, the entity object was outofsync.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 28-Apr-2006 06:17:05   

...in a web app where I don't do any inserts. What could be some reasons to get this error if there is no insert activity? Is there a timeout that occurs?

It doesn't have to be with Inserts, any persistance change done to an entity and the entity is not fetched back may cause this exception. (If you don't have a fresh valid entity)

Maybe if you post a code snippet we can identify the problem more specifically.

FatAlbert
User
Posts: 11
Joined: 10-Aug-2006
# Posted on: 21-Feb-2007 17:05:58   

I'm having the same problem. The code is very non-complex:


private Currency GetAccountCurrency(AccountEntity _account)
        {
            CurrencyEntity entity = new CurrencyEntity(_account.CurrencyId);
            base.Adapter.FetchEntity(entity);
            if (entity == null)
                return null;
            Currency currency = CurrencyFactory.CreateNew(entity.CurrencyId, entity.Name, entity.Description);
            return currency;            
        }

When trying to get entity.Name the exception is thrown. What's the problem? I've done this successfully with other entities.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Feb-2007 17:32:56   

if (entity == null)

The above check will not do the job I think you want it to do.

entity will never be null, as it's already instantiated before, if you want to check if an entity has been fetched or not, use the following:

 if(entity.Fields.State == EntityState.Fetched)
FatAlbert
User
Posts: 11
Joined: 10-Aug-2006
# Posted on: 22-Feb-2007 08:47:43   

Thanks for a quick reply. It solved my problem.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 22-Feb-2007 08:57:27   

You're mostly welcomed.

cuonglt12h
User
Posts: 1
Joined: 24-Feb-2010
# Posted on: 24-Feb-2010 09:20:27   

Walaa wrote:

You're mostly welcomed.

Hi Walaa

I'm a new Member, I have a question for you

I use LLBL Version 2.6

My DB is SQL 2008, I create Table with Primary key is nvarchar(50) ( Default Value: NewId()) I Use LLBL gen DB and My Genneration to Gen File

but when I call function Insert (Entity) after I want : Entity = _TableManager.Insert(TableEntity)

hiccccccc, why It retun Entity=null cry

You help Me (Urgent)

Thank

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 24-Feb-2010 09:55:38   

I don't know what _TableManager.Insert() does, but you'd better use unique_identifier as the type of the PK field and set the default value to: NEWSEQUENTIALID().

Please read the following, which are quoted from the manual:

Fields which get their values from a trigger, from newid() or a default constraint calling a user defined function are not considered sequenced fields and these values will not be read back, so you'll have to supply a value for these fields prior to saving the entity. This isn't true for fields which are of type unique_identifier on SqlServer 2005/2008 when the DQE is set in SqlServer 2005 compatibility mode and the field has in the database a default value of NEWSEQUENTIALID().

SqlServer specific: NEWSEQUENTIALID() support When you're using unique_identifier types for primary keys on SqlServer 2005, you can benefit from the new feature of SqlServer 2005 called NEWSEQUENTIALID(). This feature allows you to auto-generate new GUIDs for your primary keys which are sequential, so they are friendly for clustered indexes. To use this feature in LLBLGen Pro, you've to specify as the default for the primary key field in the table definition: NEWSEQUENTIALID(). Furthermore, you shouldn't set the PK field to a new GUID value when you're saving the entity. Before you save the entity set the SqlServer Dynamic Query Engine (DQE) in the SqlServer 2005 compatibility mode (see below). The DQE will then figure out to let the database insert the NEWSEQUENTIALID() produced value and it's automatically retrieved for you into the entity's PK.

P.S. Next time start your own thead, please.