AcceptChanges不能继续,因为对象的键值与ObjectStateManager中的另一个对象冲突
本文关键字:ObjectStateManager 一个对象 冲突 键值 继续 因为 对象 AcceptChanges 不能 | 更新日期: 2023-09-27 18:09:13
对数据库的更改已成功提交,但在更新对象上下文时发生错误。ObjectContext可能处于不一致的状态。内部异常消息:AcceptChanges不能继续,因为对象的键值与ObjectStateManager中的另一个对象冲突。在调用AcceptChanges之前,请确保键值是唯一的。
是我得到的错误信息。以下是我使用的两个函数…
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");
}
}
数据模型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
我有一个完全相同的场景,基于Oracle数据库的实体模型。Identity的实现是由触发器完成的,所以当向模型添加表时,它不会将Identity列的StoreGenertedPattern属性设置为Identity,因为它不知道该列是Identity。
需要打开模型编辑器,找到模型中的实体,单击键列并手动将StoreGenertedPattern属性设置为'Identity'。
我能找到的最接近的答案是:
因为Oracle使用序列+触发器使"自动识别"值,似乎当实体框架在保存它时添加对象时,返回的值仍然是0,因为触发器/序列还没有更新它。
因为0这个数字,objectmanager会认为多个实体键为0的对象存在冲突。
我没有一个"防弹"的解决方案,但我已经重写了我的解决方案,以另一种方式处理。
' T
这可能与您的问题无关,但我在一个ajax管理器运行的网页上遇到了这个问题,直到我这样做:
...
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;
}
在Page_Load中创建ObjectContext而不是回发导致了我的异常