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

前端 未结 2 1855
独厮守ぢ
独厮守ぢ 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 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.

提交回复
热议问题