6

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

Is the error message i get. here are the two functions i use...

    public IList<string> GenerateVersions(decimal id, decimal fId, string folderName, string filename, string objFile)
    {
        List<string> generatedFiles = new List<string>();

        foreach (var tCmdSets in db.IMG_SETTINGS_CMDSETS.Where("it.SETTINGS_FOLDER_ID = @folderid", new ObjectParameter("folderid", id)))
        {
            var strDestinationPath = ImageResizer.Util.PathUtils.RemoveExtension(Path.Combine(tmpDefaultFolder, tCmdSets.SETTINGS_CMDSET_DESTINATION, filename));
            ResizeSettings objResizeCommand = new ResizeSettings(tCmdSets.SETTINGS_CMDSET_COMMAND);

            var strCreatedFile = ImageBuilder.Current.Build(objFile, strDestinationPath, objResizeCommand, false, true);
            generatedFiles.Add("### File created: (" + folderName + " » " + tCmdSets.SETTINGS_CMDSET_NAME + " ») " + Path.GetFileName(strCreatedFile));

            IMG_UPLOAD_GENERATED_FILES tObjGenerated = new IMG_UPLOAD_GENERATED_FILES();

            tObjGenerated.UPLOAD_GENERATED_FILE_NAME = Path.GetFileName(strCreatedFile);
            tObjGenerated.UPLOAD_GENERATED_PATH = Path.GetDirectoryName(strCreatedFile);
            tObjGenerated.SETTINGS_CMDSET_ID = tCmdSets.SETTINGS_CMDSET_ID;
            tObjGenerated.UPLOAD_FILE_ID = fId;

            dbHandler.IMG_UPLOAD_GENERATED_FILES.AddObject(tObjGenerated);
            dbHandler.SaveChanges();
        }
        return generatedFiles;
    }

    public ActionResult UploadBulkFiles(decimal id)
    {
        IMG_SETTINGS_FOLDERS img_settings_folders = db.IMG_SETTINGS_FOLDERS.Single(i => i.SETTINGS_FOLDER_ID == id);
        string strBulkDirectory = Path.Combine(tmpDefaultFolder, img_settings_folders.SETTINGS_FOLDER_BULK);
        string[] objFiles = Directory.GetFiles(strBulkDirectory);
        List<string> lstOuput = new List<string>();

        foreach (var tFile in objFiles)
        {
            System.IO.File.Move(tFile, Path.Combine(tmpDefaultFolder, "masters", img_settings_folders.SETTINGS_FOLDER_NAME, Path.GetFileName(tFile)));

            lstOuput.Add("### File moved to masters (" + img_settings_folders.SETTINGS_FOLDER_NAME + " ») " + Path.GetFileName(tFile));

            IMG_UPLOAD_FILES tObjUploadedFile = new IMG_UPLOAD_FILES();

            tObjUploadedFile.UPLOAD_FILE_NAME = Path.GetFileName(tFile);
            tObjUploadedFile.SETTINGS_FOLDER_ID = img_settings_folders.SETTINGS_FOLDER_ID;

            dbHandler.IMG_UPLOAD_FILES.AddObject(tObjUploadedFile);
            dbHandler.SaveChanges();

            var objGeneratedFiles = GenerateVersions(img_settings_folders.SETTINGS_FOLDER_ID,tObjUploadedFile.UPLOAD_FILE_ID, img_settings_folders.SETTINGS_FOLDER_NAME, Path.GetFileName(tFile), Path.Combine(tmpDefaultFolder, "masters", img_settings_folders.SETTINGS_FOLDER_NAME, Path.GetFileName(tFile)));
            lstOuput.AddRange(objGeneratedFiles);
        }
        if (lstOuput.Count > 0)
        {
            return PartialView("UploadSingleFile", lstOuput);
        }
        else
        {
            return PartialView("NoUploads");
        }
    }

DATA MODEL

IMG_UPLOAD_FILE

  • UPLOAD_FILE_ID (PK)
  • UPLOAD_FILE_NAME
  • SETTINGS_FOLDER_ID

IMG_UPLOAD_GENERATED_FILES

  • UPLOAD_GENERATED_FILE_ID (PK)
  • UPLOAD_GENERATED_FILE_NAME
  • UPLOAD_GENERATED_FILE_PATH
  • SETTINGS_CMDSET_ID
  • UPLOAD_FILE_ID
tereško
  • 58,060
  • 25
  • 98
  • 150
Thomas Hansen
  • 193
  • 2
  • 9
  • Read this topic for answer your question http://stackoverflow.com/questions/3011764/autonumber-with-entity-framework –  Sep 19 '12 at 11:23
  • May sound silly but in our case there was an unnecessary select statement in a trigger that was making the trigger return data and the SaveChanges() actually took quite some time to execute and eventually thrown the above error. – pavan kumar Jan 28 '21 at 18:24

3 Answers3

4

I had the exact same scenario with Entity Model based on Oracle database. The implementation of Identity is done by trigger so when adding the tables to the model it does not set the StoreGenertedPattern property of the identity column to Identity since it doens't aware that this column is identity.

There is a need to open model editor, locate the entity in the model, click on the key column and set the StoreGenertedPattern property to 'Identity' manually.

Koby Mizrahy
  • 1,361
  • 2
  • 12
  • 23
3

The closest I can come to finding an answer is:

Because Oracle uses a Sequence + Trigger to make "Auto Ident" values, it seems like when the entity framework adds an object at saves it, the value return is still 0, because the trigger/sequence haven't updated it yet.

Because of the 0 number, the ObjectMannager will think that multiple objects with the entity key of 0 are in conflict.

I don't have a "bullet proof" solutions, but have rewritten my solutions to handle it another way.

\T

Thomas Hansen
  • 193
  • 2
  • 9
  • 1
    This is not an "bullet proof" solution but Thundering Storm solution. You made my day. i can not express my happiness in words. Today was the ending corner of my project submition. And got the solution with your idea. May Allah the Almighty bless you. – Muhammad Ashikuzzaman Aug 28 '15 at 06:52
0

This might not be related to your problem but I was getting this problem on a web page with an ajax manager running until I did this:

    ...
    private static _objectContext;
    protected void Page_Init(object sender, EventArgs e)
    {
        _objectContext = new ObjectContext();
    }
    ...
    protected void _ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
    {
        e.Context = _objectContext;
    }

    protected void _ContextDisposing(object sender, EntityDataSourceContextDisposingEventArgs e)
    {
        e.Cancel = true;
    }

Creating the ObjectContext in Page_Load when not a postback caused that very exception for me.

DarrenD
  • 53
  • 9