0

I am currently trying to save an EntityCollection that is populated with both new and Dirty Entity objects in different scenarios.

I have set up a transaction to roll back in the Event of failure while saving.

However, it always seems to fail and throws an Error...in both cases, saving a new or an existing EntityCollection.

I also have a method that picks and adds individual Entities i.e LanguagetranslationEntity to an Entitycollection that is defined as property in the class.

public EntityCollection<LanguageTranslationEntity> LanguagetranslationCollection { get; set; }

public void AddLanguageTranslationToCollection(LanguageTranslationEntity prompt,bool isnew)
       {
           //Add the prompt to the collection
           LanguagetranslationCollection.Add(prompt);
           Isnewcollection = isnew;
       }

However, an exception is always thrown regardless of whether i try to save new or old entities like as shown below.

An exception was caught during the execution of an action query: Violation of PRIMARY KEY constraint 'PK_LanguageTranslations'. Cannot insert duplicate key in object 'dbo.LanguageTranslations'. The duplicate key value is (translation_10374, 1).

public void SaveLanguageTranslationCollection(DataAccessAdapter adapter)
    {
        using (DataAccessAdapter newadapter = adapter)
        {
            adapter.SaveEntityCollection(LanguagetranslationCollection);
        }
    }

Should i save each Entity on its Own?? and also, how should i use the SaveEntityCollection()?

I intend to use it for saving a number of LanguageTranslationEntities by populating them into an EntityCollection and saving them all at once,using a Transaction for purposes of Rollback in the Event an Exception is thrown.

Kindly help

2 Answers2

1

The exception suggests that one of the entities inside LanguagetranslationCollection is marked as 'new' but the primary key is already used in your DB.

So, you don't have to save them individually, but it actually could help to identify what is the duplicate entity. Once you identify it, you can investigate further Why is it using an already used PK.

David Elizondo
  • 1,123
  • 1
  • 7
  • 16
  • Thankyou.that Makes sence. In what scenario is the SaveEntityCollection() method used? unfortunately there is very Little documentation on it. – Michael Ngugi M Oct 12 '13 at 16:14
0

I finally figured it out :-)

Within every transaction, one must always remember that they shouldnt have any methods reinitializing the DataaccessAdapter i.e

using(var adapter = new DataAccessAdapter())
{
 //Do saving here
 SaveLanguageTranslationCollection(adapter);
};

this is what causes the OurOfSyncException to be thrown,as the state data is cleared and initialized a new for the transaction that had been created with the initial dataAccessAdapter.

here is an Example.

public void Save(PromptEntity prompt)
        {
            using (var adapter = new DataAccessAdapter())
            {
                //start transaction
                adapter.StartTransaction(IsolationLevel.ReadCommitted, "SavePrompt");
                try
                {
                        //saving occurs here.
                        adapter.SaveEntity(prompt);
                        SaveLanguageTranslationCollection(adapter);
                        adapter.Commit();
                }
                catch (Exception)
                {
                    adapter.Rollback();
                    throw;
                }
            }

        }

you must pass the same adapter running the transaction to the methods saving. i.e

private void savetranslationprompt(LanguageTranslationEntity translationentity,
DataAccessAdapter adapter)
    {
            adapter.SaveEntity(translationentity);
    }