views:

398

answers:

3

On my development machine everything is working fine, however as soon as I publish it to the server I get

System.InvalidCastException: Specified cast is not valid

This is happening on

db.SubmitChanges();

I've check out https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=351358 and it still causes me errors

I also ran the Windows Update and everything is up to date on the server

+1  A: 
ChrisF
No date fields in the tableIt is happening all over the place not just on one submit, most of the tables it is failing to insert, the only ones it is not are very simple tables
Coppermill
@Coppermill - in that case look for the differences between the environments.
ChrisF
Okay I've been performing some testsBoth environments are 32 bitI've copied all the development files to the server and pointed the connectionstring to the server db I am running .Net 3.5 sp1 on both server and development.All reference files are held in the bin directory and are the same as on my development boxI have connected from the Dev box to the live SQL db and that works okay.I'm a little at a loss as to what to do next
Coppermill
I've had this application working without any problems just two weeks ago, and the changes I have made are not major and nothing new has been added either.This is why I am so confused.
Coppermill
@Coppermill - I can't think of anything else at the moment, sorry.
ChrisF
I have created a number of Unit Tests using nunit, testing both the models and the controllers and they work on development and the server.What other tests can I do to replicate the issue?
Coppermill
A: 

Have you changed the data type of any column in the model, or in the database? That exception is usually thrown as a result of a mismatch between the data type used in the L2S model and the data type used in the database.

Alternatively, do you have any varchar(1) or nvarchar(1) fields? If so, the L2S designer will by default map those to System.Char. This is an incorrect mapping since a varchar(1) or nvarchar(1) can contain an empty string while a System.Char must contain exactly one character, and can result in InvalidCastExceptions being thrown (although that mismatch usually occur when retrieving data, not when submitting)...

KristoferA - Huagati.com
Database's are the same, SQL 2008
Coppermill
Ok. Is the problem easy to reproduce? If so, write a code snippet that reproduces it on the server. Then update that code snippet to do a GetChangeSet right before SubmitChanges. Serialize the change set and inspect the data to see if there is something 'funny' going on. Also, if you can include the SQL-DDL for creating one of the affected tables as well as the DBML and/or class definition from the [dbmlfile].designer.cs for the same table in your question then that might shed some light on what is going on...
KristoferA - Huagati.com
+1  A: 

Okay I have resolved the issue.

What appears to be the problem is that I have tables that have Id as Int and the Primary keys and also have GUID's as a fields which is defined as Unique, and the Guid field is a key to another table and being used as a foreign key.

When I set the Guid field as the primary key in the table the problem goes away.

I've not created a test project to prove this, due to time constraints as we are going live next week

Coppermill