I. Cause of exception
"System. invalidcastexception: specified cast is not valid"
The above exception occurs in the query of the LINQ to SQL statement, usually because the field types of the relevant tables in the model file are inconsistent with those of the corresponding tables in the database.
II. Solution:
1) check whether the "server data type" attribute value in the field attribute of the relevant table in the model file (. dbml) is consistent with the database field type. If the attribute is inconsistent, modify the attribute value based on the database field type;
2) check whether the "type" attribute value in the field attribute of the relevant table in the model file (. dbml) matches the database field type. If the attribute does not match, it is modified based on the database field type.
Tip:
You can narrow the field range according to the description of the data type in the exception stacktrace information.