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.InvalidC astException
Source: System.Data.Lin q
TargetSite: Boolean TryCreateKeyFro mValues(System. Object[], V ByRef)
HelpLink: null
Stack: at
System.Data.Lin q.IdentityManag er.StandardIden tityManager.Sin gleKeyManager`2 .TryCreateKeyFr omValues(Object[] values, V& v)
at
System.Data.Lin q.IdentityManag er.StandardIden tityManager.Ide ntityCache`2.Fi nd(Object[] keyValues)
at System.Data.Lin q.IdentityManag er.StandardIden tityManager.Fin d(MetaType
type, Object[] keyValues)
at System.Data.Lin q.CommonDataSer vices.GetCached Object(MetaType type,
Object[] keyValues)
at System.Data.Lin q.ChangeProcess or.GetOtherItem (MetaAssociatio n assoc,
Object instance)
at System.Data.Lin q.ChangeProcess or.BuildEdgeMap s()
at System.Data.Lin q.ChangeProcess or.SubmitChange s(ConflictMode failureMode)
at System.Data.Lin q.DataContext.S ubmitChanges(Co nflictMode failureMode)
at System.Data.Lin q.DataContext.S ubmitChanges()
(.....)
=============== =======
This error is being thrown on the following code:
============
ResponseDataCon text db = new ResponseDataCon text(m_Connecti onString);
CodebookVersion codebookVersion = db.CodebookVers ions.Single(cv =>
cv.VersionTag == m_CodebookVersi onTag);
ResponseCode rc = new ResponseCode()
{
SurveyQuestionN ame = "Q11",
Code = 3,
Description = "Yet another code"
};
codebookVersion .ResponseCodes. Add(rc);
db.SubmitChange s(); //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 'responseCodeTa bleId' and is of type: INT
NOT NULL
codebookVersion (parent class) maps to table tblResponseCode Table
responseCode (childClass) maps to table tblResponseCode
If I execute SQL directly, it works. e.g.
INSERT INTO tblResponseCode
(responseCodeTa bleId, surveyQuestionN ame, code, description)
VALUES (13683, 'Q11', 3, 'Yet another code')
Updates to the same class work properly. e.g.
codebookVersion .ResponseCodes[0].Description = "BlahBlahBl ah";
db.SubmitChange s(); //no exception - change is committed to db
I've examined the variable, rc, after the .Add() operation and it does,
indeed, receive the proper responseCodeTab leId, just as I would expect since
I'm adding it to that collection.
tblResponseCode Table's full definition:
COLUMN_NAME TYPE_NAME
id int identity
responseCodeTab leId int
surveyQuestionN ame nvarchar
code smallint
description nvarchar
dtCreate smalldatetime
dtCreate has a default value of GetDate().
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?
--
-Paul Prewett
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.InvalidC astException
Source: System.Data.Lin q
TargetSite: Boolean TryCreateKeyFro mValues(System. Object[], V ByRef)
HelpLink: null
Stack: at
System.Data.Lin q.IdentityManag er.StandardIden tityManager.Sin gleKeyManager`2 .TryCreateKeyFr omValues(Object[] values, V& v)
at
System.Data.Lin q.IdentityManag er.StandardIden tityManager.Ide ntityCache`2.Fi nd(Object[] keyValues)
at System.Data.Lin q.IdentityManag er.StandardIden tityManager.Fin d(MetaType
type, Object[] keyValues)
at System.Data.Lin q.CommonDataSer vices.GetCached Object(MetaType type,
Object[] keyValues)
at System.Data.Lin q.ChangeProcess or.GetOtherItem (MetaAssociatio n assoc,
Object instance)
at System.Data.Lin q.ChangeProcess or.BuildEdgeMap s()
at System.Data.Lin q.ChangeProcess or.SubmitChange s(ConflictMode failureMode)
at System.Data.Lin q.DataContext.S ubmitChanges(Co nflictMode failureMode)
at System.Data.Lin q.DataContext.S ubmitChanges()
(.....)
=============== =======
This error is being thrown on the following code:
============
ResponseDataCon text db = new ResponseDataCon text(m_Connecti onString);
CodebookVersion codebookVersion = db.CodebookVers ions.Single(cv =>
cv.VersionTag == m_CodebookVersi onTag);
ResponseCode rc = new ResponseCode()
{
SurveyQuestionN ame = "Q11",
Code = 3,
Description = "Yet another code"
};
codebookVersion .ResponseCodes. Add(rc);
db.SubmitChange s(); //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 'responseCodeTa bleId' and is of type: INT
NOT NULL
codebookVersion (parent class) maps to table tblResponseCode Table
responseCode (childClass) maps to table tblResponseCode
If I execute SQL directly, it works. e.g.
INSERT INTO tblResponseCode
(responseCodeTa bleId, surveyQuestionN ame, code, description)
VALUES (13683, 'Q11', 3, 'Yet another code')
Updates to the same class work properly. e.g.
codebookVersion .ResponseCodes[0].Description = "BlahBlahBl ah";
db.SubmitChange s(); //no exception - change is committed to db
I've examined the variable, rc, after the .Add() operation and it does,
indeed, receive the proper responseCodeTab leId, just as I would expect since
I'm adding it to that collection.
tblResponseCode Table's full definition:
COLUMN_NAME TYPE_NAME
id int identity
responseCodeTab leId int
surveyQuestionN ame nvarchar
code smallint
description nvarchar
dtCreate smalldatetime
dtCreate has a default value of GetDate().
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?
--
-Paul Prewett
Comment