Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to SQL -
    primarykey
    data
    text
    <p>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:</p> <blockquote> <p>Message: Specified cast is not valid.</p> <p>Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object[], V ByRef) HelpLink: null Stack: at System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager<code>2.TryCreateKeyFromValues(Object[] values, V&amp; v) at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache</code>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()</p> <p>(.....)</p> </blockquote> <p>This error is being thrown on the following code:</p> <pre><code> ResponseDataContext db = new ResponseDataContext(m_ConnectionString); CodebookVersion codebookVersion = db.CodebookVersions.Single(cv =&gt; 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 </code></pre> <p>The tables in question have a FK relationship between the two of them.<br> The parent table's column is called 'id', is the PK, and is of type: INT NOT NULL IDENTITY<br> The child table's column is called 'responseCodeTableId' and is of type: INT NOT NULL.</p> <p>codebookVersion (parent class) maps to table tblResponseCodeTable<br> responseCode (childClass) maps to table tblResponseCode</p> <p>If I execute SQL directly, it works. e.g. </p> <pre><code>INSERT INTO tblResponseCode (responseCodeTableId, surveyQuestionName, code, description) VALUES (13683, 'Q11', 3, 'Yet another code') </code></pre> <p>Updates to the same class work properly. e.g. </p> <pre><code>codebookVersion.ResponseCodes[0].Description = "BlahBlahBlah"; db.SubmitChanges(); //no exception - change is committed to db </code></pre> <p>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.</p> <pre><code>tblResponseCodeTable's full definition: COLUMN_NAME TYPE_NAME id int identity responseCodeTableId int surveyQuestionName nvarchar code smallint description nvarchar dtCreate smalldatetime </code></pre> <p>dtCreate has a default value of GetDate().</p> <p>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.</p> <p>I've read around and seen the problem when you have a relationship to a non PK field, but that doesn't fit my case.</p> <p>Can anyone shed any light on this situation for me? What incredibly obvious thing am I missing here? </p> <p>Many thanks.<br> Paul Prewett</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload