Thomas Hansen
Thomas Hansen

Reputation: 193

AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager

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

IMG_UPLOAD_GENERATED_FILES

Upvotes: 6

Views: 6980

Answers (3)

Koby Mizrahy
Koby Mizrahy

Reputation: 1371

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.

Upvotes: 4

DarrenD
DarrenD

Reputation: 53

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.

Upvotes: 0

Thomas Hansen
Thomas Hansen

Reputation: 193

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

Upvotes: 3

Related Questions