简体   繁体   English

使用LINQ to SQL和sqlmetal的C#中的错误System.InvalidCastException:指定的强制转换无效

[英]Error in C# using LINQ to SQL and sqlmetal System.InvalidCastException: Specified cast is not valid

When running some code I've written on our production server or another developers machine it crashes on this line. 运行我在生产服务器或其他开发人员机器上编写的代码时,它会在此行崩溃。

Ac_CalcDetail xy = db.Ac_CalcDetails.Where(chld => chld.CalcIdent.Equals(CalcIdent)).First();

CalcIdent is just a guid as a string. CalcIdent只是一个字符串的向导。

I've ran sqlmetal on my local database where Ac_CalcDetail is our main table to create this strongly typed code and the class Ac_CalcDetail. 我已经在本地数据库上运行了sqlmetal,其中Ac_CalcDetail是我们的主表,用于创建此强类型代码和Ac_CalcDetail类。 The problem is when i run the code here on the production server or on one of the other developer's machines. 问题是,当我在生产服务器或另一台开发人员的计算机之一上在此处运行代码时。 We've been using LINQ for well over a year now and never had any problems so really confused about this. 我们已经使用LINQ一年多了,从来没有任何问题让它感到困惑。 Previously the exceptions I've been getting have been very specific saying which field is missing which is brilliant, but from my research apparently this error could be a certain field having the wrong datatype. 以前,我一直得到的例外非常明确,说哪个字段丢失了,哪个字段很棒,但是从我的研究来看,显然该错误可能是某个字段具有错误的数据类型。 nightmare and completelely impossible to debug through just this error. 噩梦,完全不可能仅通过此错误进行调试。

I think the only solution at present is to just back my database up in SQL Server Management Studio and then get everyone else to use this. 我认为目前唯一的解决方案是在SQL Server Management Studio中备份我的数据库,然后让其他所有人使用它。 As we're a small company we tend to just do whatever we want to our own databases, then create various SQL scripts to put on a dropbox folder to get our databases in sync (or at least what we thought was in sync). 由于我们是一家小公司,因此我们倾向于对自己的数据库做任何我们想做的事,然后创建各种SQL脚本放在一个保管箱文件夹中,以使我们的数据库同步(或至少我们认为是同步的)。 Not sure if this implies that we'll either have to stop using LINQ or start using some more advanced SQL stuff (SQL repositories or SQL database compare tools or etc). 不知道这是否意味着我们要么必须停止使用LINQ,要么开始使用一些更高级的SQL东西(SQL存储库或SQL数据库比较工具等)。

It's very odd though as I know our databases haven't been completely the same for ages (if ever) so not sure why this error is happening now and why the error message doesn't say anything! 虽然这很奇怪,但据我所知,我们的数据库已经存在了很长时间(如果有的话),所以不知道为什么现在会发生此错误,并且为什么错误消息没有说明!

Change 更改

Ac_CalcDetail xy Ac_CalcDetail xy

to

var xy var xy

than try debugging it. 而不是尝试调试它。 But clearly looks like you got some Types wrong. 但是显然您输入的类型错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq To数据集错误System.InvalidCastException:指定的强制转换无效 - Linq To Data set error System.InvalidCastException: Specified cast is not valid System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL C#System.InvalidCastException:指定的转换无效 - C# System.InvalidCastException: Specified Cast is not valid System.InvalidCastException:指定的强制转换无效的C# - System.InvalidCastException: Specified cast is not valid C# C#请求处理程序错误System.InvalidCastException:指定的强制转换无效 - C# REQUEST HANDLER ERROR System.InvalidCastException: Specified cast is not valid C#System.InvalidCastException:'指定的转换无效。' 将MySQL与Discord.Net一起使用时 - C# System.InvalidCastException: 'Specified cast is not valid.' When using MySQL with Discord.Net System.InvalidCastException:使用postgres datareader在C#中指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid in C# using postgres datareader System.InvalidCastException:指定的强制转换无效(linq查询) - System.InvalidCastException: Specified cast is not valid (linq query) XML文件错误错误:System.InvalidCastException:指定的强制转换无效 - XML File Error Error : System.InvalidCastException: Specified cast is not valid 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM