Asp.net mvc 在实体框架6中编辑对象
我有一个对象Employee,其中包含很多关于员工的信息,比如他的登录名、姓名、姓氏、电子邮件地址等 检索完员工编辑页面上输入的所有信息后,我调用以下命令来更新对象:Asp.net mvc 在实体框架6中编辑对象,asp.net-mvc,entity-framework-6,Asp.net Mvc,Entity Framework 6,我有一个对象Employee,其中包含很多关于员工的信息,比如他的登录名、姓名、姓氏、电子邮件地址等 检索完员工编辑页面上输入的所有信息后,我调用以下命令来更新对象: public Employee Edit(Employee objEmployee) // Work on EDIT { var Original = Get(objEmployee.Login); _entities.Employees.Add(objEmployee); _en
public Employee Edit(Employee objEmployee) // Work on EDIT
{
var Original = Get(objEmployee.Login);
_entities.Employees.Add(objEmployee);
_entities.SaveChanges();
return objEmployee;
}
一旦它到达SaveChanges()
我会得到以下信息:
System.Data.Entity.Infrastructure.DbUpdateException was caught
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at CRAWebSiteMVC.Models.EntityEmployeeManagerRepository.Edit(Employee objEmployee) in c:\Users\kade\Source\Workspaces\InfoCubedProjects\CRA\CRAWebSiteMVC\Models\Employee\EntityEmployeeManagerRepository.cs:line 26
at CRAWebSiteMVC.Models.EmployeeManagerService.Edit(Employee objEmployee) in c:\Users\kade\Source\Workspaces\InfoCubedProjects\CRA\CRAWebSiteMVC\Models\Employee\EmployeeManagerService.cs:line 113
InnerException: System.Data.Entity.Core.UpdateException
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__d()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClassb.<SaveChangesInternal>b__8()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
InnerException: System.Data.SqlClient.SqlException
HResult=-2146232060
Message=Violation of PRIMARY KEY constraint 'PK_EMPLOYEE'. Cannot insert duplicate key in object 'dbo.Employee'.
The statement has been terminated.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=14
LineNumber=1
Number=2627
Procedure=""
Server=MMSDEVNEW\SQL2008
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
InnerException:
捕获到System.Data.Entity.Infrastructure.DbUpdateException
HResult=-2146233087
消息=更新条目时出错。有关详细信息,请参见内部异常。
Source=EntityFramework
堆栈跟踪:
位于System.Data.Entity.Internal.InternalContext.SaveChanges()处
在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()中
位于System.Data.Entity.DbContext.SaveChanges()处
在c:\Users\kade\Source\Workspaces\InfoCubedProjects\CRA\CraWebItemVC\Models\Employee\EntityEmployeeManagerRepository.Edit(Employee objEmployee)中的crawebItemVC.Models.EntityEmployeeManagerRepository.cs:第26行
在c:\Users\kade\Source\Workspaces\InfoCubedProjects\CRA\CRAWebSiteMVC\Models\EmployeeManagerService.Edit(Employee-objeemployee)中的CRAWebSiteMVC.Models.EmployeeManagerService.Edit(Employee-objeemployee):第113行
InnerException:System.Data.Entity.Core.UpdateException
HResult=-2146233087
消息=更新条目时出错。有关详细信息,请参见内部异常。
Source=EntityFramework
堆栈跟踪:
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()处
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.b__2(UpdateTranslator ut)
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult,Func`2 updateFunction)
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()处
在System.Data.Entity.Core.Objects.ObjectContext.b_ud()中
在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,IDBEcutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项、IDBEcutionStrategy executionStrategy、Boolean startLocalTransaction)
在System.Data.Entity.Core.Objects.ObjectContext.c__DisplayClassb.b__8()中
在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1操作)中
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions选项,布尔值executeInExistingTransaction)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions)
位于System.Data.Entity.Internal.InternalContext.SaveChanges()处
InnerException:System.Data.SqlClient.SqlException
HResult=-2146232060
消息=违反主键约束“PK_EMPLOYEE”。无法在对象“dbo.Employee”中插入重复的密钥。
声明已终止。
Source=.Net SqlClient数据提供程序
错误代码=-2146232060
等级=14
行号=1
数字=2627
Procedure=“”
服务器=MMSDEVNEW\SQL2008
状态=1
堆栈跟踪:
位于System.Data.SqlClient.SqlConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)
位于System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)
位于System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject StateObjectStateObj、布尔调用方连接锁、布尔异步关闭)
位于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、String ResetOptions String)
位于System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务和任务、Boolean asyncWrite、SqlDataReader ds)
位于System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、String方法、TaskCompletionSource`1 completion、Int32超时、Task&Task、Boolean asyncWrite)
位于System.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource`1完成,字符串方法名,布尔sendToPipe,Int32超时,布尔异步写入)
位于System.Data.SqlClient.SqlCommand.ExecuteOnQuery()处
在System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b_u0(DbCommand t,DbCommandInterceptionContext`1c)中
在System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget目标,Func`3操作,TInterceptionContext拦截Context,操作`3执行,操作`3执行)
位于System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand命令,DbCommandInterceptionContext interceptionContext)
位于System.Data.Entity.Internal.InterceptableDbCommand.ExecuteOnQuery()处
在System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues,List`1 generatedValues)
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()处
内部异常:
违反主键约束“PK_EMPLOYEE”。无法在对象“dbo.Employee”中插入重复的密钥
是什么原因造成的,我该如何解决它?您需要更新实体,而不是重新登录
public Employee Edit(Employee objEmployee) // Work on EDIT
{
_entities.Employees.Attach(objEmployee);
var entity = _entities.Entry(objEmployee);
entry.State = EntityState.Modified; // Mark as modified
_entities.SaveChanges();
return objEmployee;
}