ASP.NET MVC2 Guid持久性问题

时间:2011-01-30 01:08:21

标签: sql-server entity-framework asp.net-mvc-2

我在.NET 4.0上有一个ASP.NET MVC2应用程序,使用VS 2010构建,它有两个表(以及其他表),employees表包含以下列: 名字,MiddleName,LastName,NationalID,EmployeeType,MobileNumber,Email和UserId。

userId字段是来自aspnet_users表的外键,恰好是一个guid。我正在尝试为所选用户保留员工详细信息,但由于收到错误而无法执行此操作。

可以使用ViewData字典提取和显示所选用户的guid值。 userId guid值为23aa8cd9-7552-403c-8c99-fcdd611cb188

我想将此值与其他员工属性一起保留,但是当我提交表单时,我会收到如下所示的错误。 我也在使用EF4作为我的模型。这是堆栈跟踪:

System.Data.UpdateException was unhandled by user code
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at TechDispatch.Models.EmployeeRepository.SaveChanges() in C:\Projects\TechDispatch\TechDispatch\Models\EmployeeRepository.cs:line 131
       at TechDispatch.Models.EmployeeRepository.AddNewEmployee(Employee Add) in C:\Projects\TechDispatch\TechDispatch\Models\EmployeeRepository.cs:line 127
       at TechDispatch.Controllers.EmployeeController.Create(Employee employee) in C:\Projects\TechDispatch\TechDispatch\Controllers\EmployeeController.cs:line 69
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: System.Data.SqlClient.SqlException
       Message=String or binary data would be truncated.
The statement has been terminated.
       Source=.Net SqlClient Data Provider
       ErrorCode=-2146232060
       Class=16
       LineNumber=1
       Number=8152
       Procedure=""
       Server=.
       State=13
       StackTrace:
            at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
            at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
            at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
            at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
            at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
            at System.Data.SqlClient.SqlDataReader.get_MetaData()
            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)
            at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
            at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
            at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
            at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
            at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
            at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       InnerException:

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

对我来说,这条消息意味着您要为一个char / varchar字段添加一个太长的字符串:

    String or binary data would be truncated.
    The statement has been terminated.