views:

1317

answers:

3

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:

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:

  • id = 1, name = "first"
  • id = 2, name = "second"

What could possibly be causing a casting error?

UPDATE:

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

[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

UPDATE 2:

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.

+1  A: 

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.

Jason
No foreign keys, but there is an association (this table is the child of another table). When I remove the association I can delete, so the question now is why the association is causing the error.
gfrizzle
Yep, this is the exact bug I'm hitting. Fortunately I was able to refactor the table to use different fields in the association to achieve the same results to get around it. This is very good to know as it may be a while before we can move to .NET 4.0. Thanks.
gfrizzle
A: 

Try:

dc.MyTables.DeleteOnSubmit(dc.MyTables.SingleOrDefault(x=>x.id==1));
dc.MyTables.DeleteAllOnSubmit(dc.MyTables);
Francisco
Somehow I don't think converting from VB to C# is going to solve the problem. :)
gfrizzle
oops sorry, nvm then =)
Francisco
+1  A: 

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

Glen Little
I erease only my link relations and working fine, like your article suggest.
Cédric Boivin