Linq to Sql Exception Specified cast is not valid

B

Bill McCormick

I'm getting an exception *Specified cast is not valid* after calling
SubmitChanges for a DB insert. Seems to be some strange issue with an
association to a table. I've seen some postings from about a year ago that
some others were having the same problem, bu I didn't see any resolution.

Maybe this is a common problem that somebody knows about and could save me
the trouble of having to post all the details of my schema and code.

Thanks,

Bill
 
M

Mr. Arnold

Bill McCormick said:
I'm getting an exception *Specified cast is not valid* after calling
SubmitChanges for a DB insert. Seems to be some strange issue with an
association to a table. I've seen some postings from about a year ago that
some others were having the same problem, bu I didn't see any resolution.

Maybe this is a common problem that somebody knows about and could save me
the trouble of having to post all the details of my schema and code.

The class object based on the schema has null data, the data for a given
field based on the schema is an invalid type of data or the class object
itself is not the expected type of class.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4197 (20090629) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
B

Bill McCormick

I'm getting an exception *Specified cast is not valid* after calling
SubmitChanges for a DB insert. Seems to be some strange issue with an
association to a table. I've seen some postings from about a year ago
that some others were having the same problem, bu I didn't see any
resolution.

Maybe this is a common problem that somebody knows about and could save
me the trouble of having to post all the details of my schema and code.

Here's the stacktrace:

at
System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[]
values, V& v)
at
System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[]
keyValues)
at
System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type,
Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type,
Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc,
Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at ScAggScale.AppPages.NewTicket.btnTar_Click(Object sender,
RoutedEventArgs e) in mysourcefile.xaml.cs:line 104

I googled on TryCreateKeyFromValues and found a few items that indicate this
may be a Linq bug and I might be able SP my way out of it. I thought I had
all the latest stuff ?!?!
 
B

Bill McCormick

I'm getting an exception *Specified cast is not valid* after calling
SubmitChanges for a DB insert. Seems to be some strange issue with an
association to a table. I've seen some postings from about a year ago
that some others were having the same problem, bu I didn't see any
resolution.

Maybe this is a common problem that somebody knows about and could
save me the trouble of having to post all the details of my schema and
code.

Here's the stacktrace:

at
System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[]
values, V& v)
at
System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[]
keyValues)
at
System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType
type, Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type,
Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation
assoc, Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode
failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at ScAggScale.AppPages.NewTicket.btnTar_Click(Object sender,
RoutedEventArgs e) in mysourcefile.xaml.cs:line 104

I googled on TryCreateKeyFromValues and found a few items that indicate
this may be a Linq bug and I might be able SP my way out of it. I
thought I had all the latest stuff ?!?!

Installed VS2008 SP1 and no change; same problem.

Can I get some MSDN help on this please.

Thanks,

Bill
 
B

Bill McCormick

The class object based on the schema has null data, the data for a
given field based on the schema is an invalid type of data or the class
object itself is not the expected type of class.

Obviously. However, since I can make this problem disappear by removing an
Linq to SQL association (on a table my code is not even tying to update), I
think it runs a little deeper.

Thanks Anyway.
 
M

Mr. Arnold

Bill McCormick said:
Obviously. However, since I can make this problem disappear by removing an
Linq to SQL association (on a table my code is not even tying to update),
I think it runs a little deeper.

Thanks Anyway.

I remove all associations off the ADO.NET Entity Framework Entity Model to
avoid associations placed on the entities due to constraints and due to
navigation paths the model may take that can lead to a constraint table
being inserted into or updated based on the navigation path from another
table being inserted into or updated.

One can take full control of the EF model. It doesn't look like one can do
that with a Link-2-SQL model.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4198 (20090629) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
P

Patrice

Could it be that the datatype for the fk and the pk doesn't match ? Which db
are you using ? Do you have a fk constraint defined in the db ?
 
V

vanderghast

I get that error, sometimes, in the automatically generated code, when a
null value is involved. Have to explicitly test the IsFieldNull before to
avoid getting that error (bad design, but I think it predates nullable
valuetype with the framework, so I excuse those who wrote that code
generator).

Vanderghast, Access MVP
 
B

Bill McCormick

I remove all associations off the ADO.NET Entity Framework Entity Model
to avoid associations placed on the entities due to constraints and due
to navigation paths the model may take that can lead to a constraint
table being inserted into or updated based on the navigation path from
another table being inserted into or updated.

One can take full control of the EF model. It doesn't look like one can
do that with a Link-2-SQL model.

Sorry, I'm having a difficult time parsing what you said. Maybe the comma
key is broken on your keyboard? ;)

Anyway, the associations should work as advertised; if not, the EF is broken
and MS needs to fix it.


Thanks
 
B

Bill McCormick

Could it be that the datatype for the fk and the pk doesn't match ? Which db
are you using ? Do you have a fk constraint defined in the db ?

Using MS Sql Server Express. The fk/pk datatype is VarChar(10). How can I
tell if I have a fk constraint?
 
B

Bill McCormick

I get that error, sometimes, in the automatically generated code, when a
null value is involved. Have to explicitly test the IsFieldNull before
to avoid getting that error (bad design, but I think it predates
nullable valuetype with the framework, so I excuse those who wrote that
code generator).

My code does not insert or update records in the table that is linked to
with the association in question. Maybe an illustration would be in order?
There are 3 tables in this example, X, Y ,Z.

X->Y, X<-Z (- = one; <,> = many)


The code sort of looks like this, where now x,y and z are Entity objects of
the X,Y & Z Entity collections/lists:

x = comboBox.SelectedItem

y.field1 = val1
y.field2 = val2
y.field3 = val3

x.Y.Add(y)

db.SubmitChanges()

This works unless there is record in table z for the record x.

I stop here in the interest of not creating too much fog.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top