Mombu the Microsoft Forum sponsored links

Go Back   Mombu the Microsoft Forum > Microsoft > LINQ Problem - Specified cast is not valid
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 7th July 03:38
paul prewett–)äõÊ&šéâ·)è²–¦
External User
 
Posts: 1
Default LINQ Problem - Specified cast is not valid



I'm attempting to use LINQ to insert a record into a child table and I'm
receiving a "Specified cast is not valid" error that has something to do w/
the keys involved. The stack trace is:

======================
Message: Specified cast is not valid.

Type: System.InvalidCastException
Source: System.Data.Linq
TargetSite: Boolean TryCreateKeyFromValues(System.Object[], V ByRef)
HelpLink: null
Stack: at
System.Data.Linq.IdentityManager.StandardIdentityM anager.SingleKeyManager`2.TryCreateKeyFromValues(O bject[] values, V& v)
at
System.Data.Linq.IdentityManager.StandardIdentityM anager.IdentityCache`2.Find(Object[] keyValues)
at System.Data.Linq.IdentityManager.StandardIdentityM anager.Find(MetaType
type, Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObjec t(MetaType type,
Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(Meta Association assoc,
Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(Con flictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(Conflic tMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()

(.....)

======================

This error is being thrown on the following code:
============
ResponseDataContext db = new ResponseDataContext(m_ConnectionString);
CodebookVersion codebookVersion = db.CodebookVersions.Single(cv =>
cv.VersionTag == m_CodebookVersionTag);
ResponseCode rc = new ResponseCode()
{
SurveyQuestionName = "Q11",
Code = 3,
Description = "Yet another code"
};
codebookVersion.ResponseCodes.Add(rc);
db.SubmitChanges(); //exception gets thrown here
============

The tables in question have a FK relationship between the two of them.
The parent table's column is called 'id', is the PK, and is of type: INT
NOT NULL IDENTITY
The child table's column is called 'responseCodeTableId' and is of type: INT
NOT NULL

codebookVersion (parent class) maps to table tblResponseCodeTable
responseCode (childClass) maps to table tblResponseCode

If I execute SQL directly, it works. e.g.
INSERT INTO tblResponseCode
(responseCodeTableId, surveyQuestionName, code, description)
VALUES (13683, 'Q11', 3, 'Yet another code')


Updates to the same class work properly. e.g.
codebookVersion.ResponseCodes[0].Description = "BlahBlahBlah";
db.SubmitChanges(); //no exception - change is committed to db

I've examined the variable, rc, after the .Add() operation and it does,
indeed, receive the proper responseCodeTableId, just as I would expect since
I'm adding it to that collection.

The only other bit of useful information that I can think of is that no SQL
is ever tried against the database, so LINQ is blowing up before it ever
tries (hence the error not being a SqlException). I've profiled and verified
that no attempt is made to execute any statements on the database.


Can anyone shed any light on this situation for me? What incredibly obvious
thing am I missing here?

Thanks very much.

--
-Paul Prewett
  Reply With Quote


  sponsored links


2 13th July 20:38
paul prewett–)äõÊ&šéâ·)è²–¦
External User
 
Posts: 1
Default LINQ Problem - Specified cast is not valid



One additional bit of useful information. tblResponseCodeTable's full
definition:

COLUMN_NAME TYPE_NAME
id int identity
responseCodeTableId int
surveyQuestionName nvarchar
code smallint
description nvarchar
dtCreate smalldatetime

dtCreate has a default value of GetDate().

Thanks.


--
-Paul Prewett
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666