SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityOutOfSyncException

Posts   
 
    
Jannik79
User
Posts: 16
Joined: 18-Feb-2007
# Posted on: 18-Feb-2007 00:56:03   

Hi

I'am getting a SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityOutOfSyncException. I have tried to read some of the other threads in the forum about this problem - but have found no solution to my problem :-(

My code looks like this (I have marked the code snippet with bold text). For some reason I can't create a new entity of AnnualAccount and save it - I then get the above exception.


// Create new annual account and new accounts (if account template is selected)

        // Store current/old profile CurrencyID
        int _oldcurrencyid = Convert.ToInt32(Profile.CurrencyID);

        // Create adapter for fetching and the transaction. 
        DataAccessAdapter adapter = new DataAccessAdapter();
        // Start the transaction.
        adapter.StartTransaction(IsolationLevel.ReadCommitted, "TwoUpates");
        try
        {
            // Fetch new annual account entity
            AnnualAccountsEntity annualaccount = new AnnualAccountsEntity();
            adapter.FetchEntity(annualaccount);
        
            // Change annual account data
            [b]annualaccount.CompanyId = Convert.ToInt32(Profile.CompanyID);
            annualaccount.AnnualAccountname = txtAnnualAccountname.Text;
            annualaccount.Startdate = Convert.ToDateTime(txtStartdate.Text);
            annualaccount.Enddate = Convert.ToDateTime(txtEnddate.Text);
            annualaccount.CurrencyId = Convert.ToInt32(ddlCurrency.SelectedValue);[/b]

            // Save annual account entity
            [b]adapter.SaveEntity(annualaccount);[/b]

            // Change profile settings based on new annual account
            Profile.AnnualAccountID = annualaccount.AnnualAccountId;
            Profile.AccountStartdate = Convert.ToString(annualaccount.Startdate);
            Profile.AccountEnddate = Convert.ToString(annualaccount.Enddate);
            Profile.CurrencyID = annualaccount.CurrencyId;

            if (Convert.ToInt32(ddlSelectAccountTemplate.SelectedValue) != 0)
            {
                // User has selected account template to use in annual account
                // Fetch selected account template
                IRelationPredicateBucket filter = new RelationPredicateBucket();
                filter.Relations.Add(AccountTemplatesEntity.Relations.AccountTemplateListEntityUsingAccountTemplateListIdCulture);
                //filter.PredicateExpression.Add(AccountTemplateListFields.AccountTemplateListId == Convert.ToInt32(ddlSelectAccountTemplate.SelectedValue) | AccountTemplatesFields.Culture == Convert.ToString(ddlSelectAccountTemplate.SelectedValue));
                filter.PredicateExpression.Add(AccountTemplateListFields.AccountTemplateListId == Convert.ToInt32(ddlSelectAccountTemplate.SelectedValue));
                filter.PredicateExpression.AddWithAnd(AccountTemplateListFields.Culture == Convert.ToString(ddlSelectAccountTemplate.SelectedValue));
                EntityCollection accounttemplate = new EntityCollection(new AccountTemplatesEntityFactory());
                adapter.FetchEntityCollection(accounttemplate, filter);
                
                foreach (AccountTemplatesEntity collection in accounttemplate)
                {
                    // Fetch new account entity
                    AccountsEntity account = new AccountsEntity();
                    adapter.FetchEntity(account);

                    // Change account entity based on current template account
                    account.CompanyId = Convert.ToInt32(Profile.CompanyID);
                    account.Accountname = collection.Accountname;
                    account.Accountnumber = collection.Accountnumber;
                    account.AnnualAccountId = annualaccount.AnnualAccountId;
                    account.AccountTypeId = collection.AccountTypeId;

                    if (collection.AccountTypeId == 3)
                    {
                        // Sum accounttype
                        account.SumFromAccountId = collection.SumFromAccountTemplateId; 
                    }

                    // Save account entity
                    adapter.SaveEntity(account);
                }

                // Fetch selected vat account template
                IRelationPredicateBucket filter2 = new RelationPredicateBucket();
                filter2.Relations.Add(VatAccountTemplatesEntity.Relations.AccountTemplateListEntityUsingAccountTemplateListIdCulture);
                filter2.PredicateExpression.Add(AccountTemplateListFields.AccountTemplateListId == Convert.ToInt32(ddlSelectAccountTemplate.SelectedValue));
                filter2.PredicateExpression.AddWithAnd(AccountTemplateListFields.Culture == Convert.ToString(ddlSelectAccountTemplate.SelectedValue));
                EntityCollection vataccounttemplate = new EntityCollection(new VatAccountTemplatesEntityFactory());
                adapter.FetchEntityCollection(vataccounttemplate, filter2);

                if (vataccounttemplate.Count != 0)
                {
                    foreach (VatAccountTemplatesEntity vatcollection in vataccounttemplate)
                    {
                        // Fetch new vataccount entity
                        VatAccountsEntity vataccount = new VatAccountsEntity();
                        adapter.FetchEntity(vataccount);

                        // Change vat account entity based on current vat account template
                        vataccount.CompanyId = Convert.ToInt32(Profile.CompanyID);
                        vataccount.Description = vatcollection.Description;
                        vataccount.SalesCode = vatcollection.SalesCode;
                        vataccount.SalesAccountId = vatcollection.SalesAccountId;
                        vataccount.PurchaseCode = vatcollection.PurchaseCode;
                        vataccount.PurchaseAccountId = vatcollection.PurchaseAccountId;

                        // Save vat account entity
                        adapter.SaveEntity(vataccount);

                        // Fetch new vatrates entity
                        VatRatesEntity vatrate = new VatRatesEntity();
                        adapter.FetchEntity(vatrate);

                        // Change vat rate entity
                        vatrate.VatId = vataccount.VatId;
                        vatrate.Startdate = vatcollection.Startdate;
                        vatrate.Rate = vatcollection.Rate;

                        // Save vat rate entity
                        adapter.SaveEntity(vatrate);
                    }   
                }
                

            }

            // Commit data
            adapter.Commit();
        }
        catch
        {
            // Abort, roll back the transaction
            adapter.Rollback();

            // Change back profile settings
            Profile.AnnualAccountID = 0;
            Profile.AccountStartdate = "";
            Profile.AccountEnddate = "";
            Profile.CurrencyID = _oldcurrencyid;

            // Bubble up exception
            throw;
        }
        finally
        {
            // Clean up. Necessary action.
            adapter.Dispose();

            //Response.Redirect("~\\Default.aspx");
        }

I have tried to shorten the code down to see if it's because of other code. My code now looks like this. But I still get the exception - or that means I only get it if I look in the watch. The entity is actually saved to the database, but thats only because I don't have the try, catch, throw statement included


DataAccessAdapter adapter = new DataAccessAdapter();
        
        // Fetch new annual account entity
        AnnualAccountsEntity annualaccount = new AnnualAccountsEntity();
        adapter.FetchEntity(annualaccount);

        // Change annual account data
        annualaccount.IsNew = true;
        annualaccount.CompanyId = Convert.ToInt32(Profile.CompanyID);
        annualaccount.AnnualAccountname = txtAnnualAccountname.Text;
        annualaccount.Startdate = Convert.ToDateTime(txtStartdate.Text);
        annualaccount.Enddate = Convert.ToDateTime(txtEnddate.Text);
        annualaccount.CurrencyId = Convert.ToInt32(ddlCurrency.SelectedValue);

        // Save annual account entity
        adapter.SaveEntity(annualaccount, true);

Can anyone help me out with this one?

Thanks

Jannik

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Feb-2007 06:15:19   

Jannik,

I think your problem it's like thread http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=3918&HighLight=1.

I can see a fragment on your 1st code snippet where likely is the error:

 // Save vat account entity
adapter.SaveEntity(vataccount);

// Fetch new vatrates entity
VatRatesEntity vatrate = new VatRatesEntity();
adapter.FetchEntity(vatrate);

// Change vat rate entity
vatrate.VatId = vataccount.VatId;

As you can see, the _vtaccount _entity **isn't refetched **(out of sync) so when you use it 7 lines forward throws the exception. You need to specify a _adapter.SaveEntity(vataccount,true); _to can reference the updated entity.

Your second code snippet seems ok, however at these lines

AnnualAccountsEntity annualaccount = new AnnualAccountsEntity();
adapter.FetchEntity(annualaccount);

// Change annual account data
annualaccount.IsNew = true;

seems to me not necessary to fetch entity (only used to retrieve existing data from DB) and set isNew=true ( new AnnualAccountsEntity() do that for you). simple_smile

David Elizondo | LLBLGen Support Team
Jannik79
User
Posts: 16
Joined: 18-Feb-2007
# Posted on: 18-Feb-2007 18:29:34   

Thanks daelmo - you are a genius! It solved my problem :-)

Jannik