LINQ to SQL删除生成“指定的强制转换无效”错误

时间:2022-05-11 20:54:32

I've got a painfully simple table that is giving me a "Specified cast is not valid" error when I try to delete one or more rows. The table has two columns, an "id" as the primary key (INT), and a "name" (VARCHAR(20)), which maps to a String in the LINQ to SQL dbml file. Both of these statements produce the error:

当我尝试删除一行或多行时,我有一个非常简单的表,它给了我一个“指定的强制转换无效”错误。该表有两列,一个“id”作为主键(INT),一个“name”(VARCHAR(20)),它映射到LINQ to SQL dbml文件中的String。这两个语句都会产生错误:

dc.DeleteOnSubmit(dc.MyTables.Where(Function(x) x.id = 1).SingleOrDefault)
dc.DeleteAllOnSubmit(dc.MyTables)

I iterated through "MyTable" just to make sure there was no weird data, and there are only two rows:

我遍历“MyTable”只是为了确保没有奇怪的数据,并且只有两行:

  • id = 1, name = "first"
  • id = 1,name =“first”

  • id = 2, name = "second"
  • id = 2,name =“second”

The exception is happening on SubmitChanges. Here is the stack trace:

SubmitChanges发生异常。这是堆栈跟踪:

[InvalidCastException: Specified cast is not valid.]
   System.Data.Linq.SingleKeyManager`2.TryCreateKeyFromValues(Object[] values, V& v) +59
   System.Data.Linq.IdentityCache`2.Find(Object[] keyValues) +28
   System.Data.Linq.StandardIdentityManager.Find(MetaType type, Object[] keyValues) +23
   System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues) +48
   System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance) +142
   System.Data.Linq.ChangeProcessor.BuildEdgeMaps() +233
   System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +59
   System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +331
   System.Data.Linq.DataContext.SubmitChanges() +19
   InpatientCensus.MaintenanceController.DeleteSoleCommunity(Int32 id) in C:\Documents and Settings\gregf\My Documents\Projects\InpatientCensus\InpatientCensus\Controllers\MaintenanceController.vb:14
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +128
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
   System.Web.Mvc.Controller.ExecuteCore() +126
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Removing the association added to the DBML file allows rows to be deleted. Why would the association be causing the error? The associated columns are both VARCHAR(20) in the database and resolve to Strings in the DBML file.

删除添加到DBML文件的关联允许删除行。为什么关联会导致错误?关联的列在数据库中都是VARCHAR(20),并解析为DBML文件中的字符串。

What could possibly be causing a casting error?

什么可能导致铸造错误?

3 个解决方案

#1


Okay, upon seeing the update, the issue is the association between your two tables. This is a known bug in .NET 3.5 SP1 and will be fixed in .NET 4.0. Try your code on a .NET 4.0 Beta if you can.

好的,看到更新后,问题就是两个表之间的关联。这是.NET 3.5 SP1中的已知错误,将在.NET 4.0中修复。如果可以,请尝试在.NET 4.0 Beta上运行代码。

#2


We had a similar problem, caused by using non-integer keys. Details and hotfix number are here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=351358

我们遇到了类似的问题,这是由使用非整数键引起的。详细信息和修补程序编号在这里:https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx ?FeedbackID = 351358

#3


Try:

dc.MyTables.DeleteOnSubmit(dc.MyTables.SingleOrDefault(x=>x.id==1));
dc.MyTables.DeleteAllOnSubmit(dc.MyTables);

#1


Okay, upon seeing the update, the issue is the association between your two tables. This is a known bug in .NET 3.5 SP1 and will be fixed in .NET 4.0. Try your code on a .NET 4.0 Beta if you can.

好的,看到更新后,问题就是两个表之间的关联。这是.NET 3.5 SP1中的已知错误,将在.NET 4.0中修复。如果可以,请尝试在.NET 4.0 Beta上运行代码。

#2


We had a similar problem, caused by using non-integer keys. Details and hotfix number are here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=351358

我们遇到了类似的问题,这是由使用非整数键引起的。详细信息和修补程序编号在这里:https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx ?FeedbackID = 351358

#3


Try:

dc.MyTables.DeleteOnSubmit(dc.MyTables.SingleOrDefault(x=>x.id==1));
dc.MyTables.DeleteAllOnSubmit(dc.MyTables);