“Specified cast is not valid” error when saving LINQ-To-SQL entity

前端 未结 2 1832
独厮守ぢ
独厮守ぢ 2021-01-13 07:34

App Details: C#, ASP.NET MVC, SQL Server 2008 ( Same version & SP level), Linq-To-SQL ORM

I\'m trying to diagnose an exception I\'m receiving:

相关标签:
2条回答
  • 2021-01-13 07:48

    This is a well-known bug in LINQ to SQL.

    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=351358

    There is a hotfix for the issue.

    http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=963657&kbln=en-us

    0 讨论(0)
  • 2021-01-13 08:10

    If that key exists in a defined relationship and the object is already created then the LINQ-provided relationship path (obj.Foo = new Foo { ... }) must be used. (I would use the provided relationships even for new objects). Failure to do so will result -- speaking from experience -- in "random" errors like that.

    Another issue that can cause that (or a very similar error) is reversed relationship columns, for compound relationships. However, if the generated code is the same and the DB is the same (for real, on both accounts) then I suspect the first reason is the culprit.

    Happy SQL-to-LINQ pothole jumping.

    0 讨论(0)
提交回复
热议问题