【问题标题】:Can you help me with this Entity Framework Error?你能帮我解决这个实体框架错误吗?
【发布时间】:2014-06-15 20:23:46
【问题描述】:

我正在做我的项目,但我不知道这个错误是什么。

请帮助我了解此代码和此错误。

TbStudy Ts = new TbStudy();

int yearselect = Convert.ToInt16(cmbyear.SelectedItem);
int weekselect = Convert.ToInt16(cmbyear.SelectedItem);

string monthselect = cmbmonth.SelectedItem.ToString();
string dayselect = cmbday.SelectedItem.ToString();

Ts.Coursename = txtcoursename.Text;
Ts.Time = Convert.ToInt16(txttimestudy.Text);
Ts.Discription = txtdiscription.Text;
Ts.Year = (from p in db.TbYears
           where p.Year == yearselect
           select p.Year).SingleOrDefault();
Ts.Month = (from p in db.TbMonths
            where p.Month == monthselect
            select p.Month).SingleOrDefault();
Ts.Weeknum = (from p in db.TbWeeks
              where p.Week == weekselect
              select p.Week).SingleOrDefault();
Ts.Day = (from p in db.TbDays
          where p.Day == dayselect
          select p.Day).SingleOrDefault();

db.AddToTbStudies(Ts);
db.SaveChanges();

此错误的消息是:

更新条目时出错。有关详细信息,请参阅内部异常。

InnerException 是:

System.Data.UpdateException 未处理
HResult=-2146233087
Message=更新条目时发生错误。有关详细信息,请参阅内部异常。
源=System.Data.Entity
堆栈跟踪:
在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter 适配器) 在 System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) 在 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions 选项) 在 System.Data.Objects.ObjectContext.SaveChanges() 在 Office_Of_Planning.IDEU_Form.btnInsert_Click(Object sender, EventArgs e) 在 C:\Users\Pooya\Documents\Visual Studio 2010\Projects\امتحان مباحث\Office_Of_Planning\Office_Of_Planning\IDEU_Form.cs:line 49 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.RunDialog(窗体) 在 System.Windows.Forms.Form.ShowDialog(IWin32Window 所有者) 在 System.Windows.Forms.Form.ShowDialog() 在 Office_Of_Planning.Main_Form.درجمطالعهToolStripMenuItem_Click(Object sender, EventArgs e) 在 C:\Users\Pooya\Documents\Visual Studio 2010\Projects\امتحان مباحث\Office_Of_Planning\Office_Of_Planning\Main_Form.cs:line 45 在 System.Windows.Forms.ToolStripItem.RaiseEvent(对象键,EventArgs e) 在 System.Windows.Forms.ToolStripMenuItem.OnClick (EventArgs e) 在 System.Windows.Forms.ToolStripItem.HandleClick (EventArgs e) 在 System.Windows.Forms.ToolStripItem.HandleMouseUp (MouseEventArgs e) 在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,ToolStripItemEventType 遇到) 在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,ToolStripItemEventType 遇到) 在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) 在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ScrollableControl.WndProc(消息和 m) 在 System.Windows.Forms.ToolStrip.WndProc(消息和 m) 在 System.Windows.Forms.ToolStripDropDown.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 Office_Of_Planning.Program.Main() 在 C:\Users\Pooya\Documents\Visual Studio 2010\Projects\امتحان مباحث\Office_Of_Planning\Office_Of_Planning\Program.cs:line 18 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:System.Data.SqlClient.SqlException H结果=-2146232060 消息=当 IDENTITY_INSERT 设置为 OFF 时,无法在表“TbStudy”中插入标识列的显式值。 Source=.Net SqlClient 数据提供者 错误代码=-2146232060 班级=16 行号=1 数量=544 程序="" 服务器=。 状态=1 堆栈跟踪: 在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔 breakConnection,Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用者HasConnectionLock,布尔异步关闭) 在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean & dataReady) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串 resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,布尔异步,Int32 超时,任务和任务,布尔异步写入,SqlDataReader ds) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,字符串方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 完成,字符串 methodName,布尔 sendToPipe,Int32 超时,布尔 asyncWrite) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator 翻译器,EntityConnection 连接,Dictionary2 identifierValues, List1 generatedValues) 在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager,IEntityAdapter 适配器) 内部异常:

【问题讨论】:

  • 你能发布内部异常吗?
  • 你看到内部异常的细节了吗?
  • 是的,我看到了内部异常,但对这个错误没有帮助。

标签: entity-framework c#-4.0


【解决方案1】:

异常表明您正尝试为标识列插入显式值。

Cannot insert explicit value for identity column in table 'TbStudy' 

也许您需要查看您的表格设计并检查键是什么。

【讨论】:

  • 也许您应该告诉 OP 将身份密钥列注释为 DatabaseGeneratedOption.Identity
  • 能否请您指出您是否认为答案有用。您是否需要更多信息。请提供反馈。
猜你喜欢
  • 1970-01-01
  • 2013-01-28
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2011-01-24
  • 2021-08-14
相关资源
最近更新 更多