Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ到SQL-_Linq_Linq To Sql - Fatal编程技术网

LINQ到SQL-

LINQ到SQL-,linq,linq-to-sql,Linq,Linq To Sql,我正试图使用LINQ将一条记录插入到子表中,我 接收到“指定的强制转换无效”错误,该错误与w/ 涉及的钥匙。堆栈跟踪是: 消息:指定的强制转换无效 类型:System.InvalidCastException 来源:System.Data.Linq TargetSite: 布尔值 TryCreateKeyFromValues(System.Object[], V ByRef)帮助链接:空堆栈:位于 System.Data.Linq.IdentityManager.StandardIdentity

我正试图使用LINQ将一条记录插入到子表中,我 接收到“指定的强制转换无效”错误,该错误与w/ 涉及的钥匙。堆栈跟踪是:

消息:指定的强制转换无效

类型:System.InvalidCastException 来源:System.Data.Linq TargetSite: 布尔值 TryCreateKeyFromValues(System.Object[], V ByRef)帮助链接:空堆栈:位于 System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager
2.TryCreateKeyFromValues(对象[])
价值,V&V)在
System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache
2.Find(对象[] 关键字值)在 System.Data.Linq.IdentityManager.StandardIdentityManager.Find(元类型 类型,对象[]键值)位于 System.Data.Linq.CommonDataServices.GetCachedObject(元类型 类型,对象[]键值)位于 System.Data.Linq.ChangeProcessor.GetOtherItem(元关联 assoc,对象实例)位于 System.Data.Linq.ChangeProcessor.BuildEdgeMaps() 在 System.Data.Linq.ChangeProcessor.SubmitChanges(冲突模式 故障模式)在 System.Data.Linq.DataContext.SubmitChanges(冲突模式 故障模式)在 System.Data.Linq.DataContext.SubmitChanges()文件

(……)

正在以下代码上引发此错误:

 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
有问题的表在它们两个表之间具有FK关系。
父表的列称为“id”,是主键,类型为:INT NOT NULL IDENTITY
子表的列称为“responseCodeTableId”,类型为:INT NOT NULL

codebookVersion(父类)映射到表tblResponseCodeTable
responseCode(childClass)映射到表TBLRResponseCode

如果我直接执行SQL,它就会工作。e、 g

INSERT INTO tblResponseCode 
(responseCodeTableId, surveyQuestionName, code, description)
VALUES (13683, 'Q11', 3, 'Yet another code')
对同一类的更新工作正常。e、 g

codebookVersion.ResponseCodes[0].Description = "BlahBlahBlah";
db.SubmitChanges(); //no exception - change is committed to db
在.Add()操作之后,我检查了变量rc,它确实收到了正确的responseCodeTableId,正如我将它添加到该集合中时所期望的那样

tblResponseCodeTable's full definition:
COLUMN_NAME TYPE_NAME
id                  int identity
responseCodeTableId int
surveyQuestionName  nvarchar
code                smallint
description         nvarchar
dtCreate            smalldatetime
dtCreate的默认值为GetDate()

我能想到的另外一点有用的信息是没有SQL 曾经尝试过数据库,所以LINQ在它之前就爆炸了 尝试(因此错误不是SqlException)。我已经分析并验证了 不尝试在数据库上执行任何语句

我读过很多书,看到了与非PK字段有关系时的问题,但这不符合我的情况

有人能帮我解释一下这种情况吗?我在这里遗漏了什么显而易见的东西

非常感谢。
保罗·普雷维特

ResponseCode rc = new ResponseCode()
    {
       SurveyQuestionName = "Q11",
       Code = 3,
       Description = "Yet another code"
    };
以及:

如果不相同,则不传递外键引用。现在,我在LINQ2SQL上拥有巨大的n00b,但我敢打赌LINQ2SQL不够聪明,无法为您做到这一点,它希望它作为匿名字典的第一个参数,并试图将字符串转换为整数

只是一些想法。

此块:

codebookVersion.ResponseCodes.Add(rc);
db.SubmitChanges(); //exception gets thrown here
您可以尝试
InsertOnSubmit
而不是
Add
?i、 e

codebookVersion.ResponseCodes.InsertOnSubmit(rc);

我认为如果我记性好的话,
Add
并不是用来插入记录的。InsertOnSubmit是用来缩小罪魁祸首范围的工具

您是否尝试过用以下内容替换匿名词典:

ResponseCode rc = new ResponseCode();

rc.SurveyQuestName = "Q11";
rc.Code = 3;
rc.Description = "Yet Another Code";

我还没有真正使用.NET 3.5(day job仍然都是2.0),所以我想知道使用匿名字典传递数据是否有问题(案例与SQL列不匹配)。

发布父表的模式

如果你看这里,其他人也有你的问题。


Linq2SQL似乎无法将某些外键映射到某些主键。一个人有一个解决方案,但我认为你已经映射到一个身份栏了。

是的,我读过这篇文章和其他文章,但它似乎总是涉及到一个人链接到一个只有唯一约束的字段。或者在这个家伙的情况下(听起来和我的一模一样),他没有得到解决方案

这是父表:

tblResponseTable definition (which maps to CodebookVersion)
COLUMN_NAME TYPE_NAME
id  int identity
versionTag  nvarchar
responseVersionTag  nvarchar

versionTag上确实有一个独特的约束,但在LINQ到SQL的东西中我看不到它的任何地方,因为没有任何东西进入数据库。。。仍然卡住。

因为没有调用数据库,所以我认为您必须查看linq到sql正在使用的映射。这个协会是什么样子的?父类和子类都应该有关联。 看看这两个类之间的linq到sql关联。该关联应具有ThisKey属性。我认为,失败的强制转换是尝试强制转换ThisKey指向的属性的值。 据我所知,当有多个键并且第一个键的类型与该键指向的类型不匹配时,可能会出现问题。我不确定linq将如何确定第一个键是什么。 从外观上看,你只有一个键和一个外键,所以这不应该是问题,但设计师,如果你正在使用它,已经知道会有创意。
我猜得差不多,但这看起来像是我以前见过的东西。

迈克,我听到了。但无论我往哪里看,一切看起来都是正确的。我已经检查并再次检查了ResponseTableId是否为int,Id是否为int。它们在设计器中被定义为int。当我查看生成的代码时,一切看起来都正常

我检查过这些关联。这是:

[Table(Name="dbo.tblResponseCode")]
public partial class ResponseCode : ...
    ...
   [Association(Name="CodebookVersion_tblResponseCode", Storage="_CodebookVersion", ThisKey="ResponseCodeTableId", OtherKey="Id", IsForeignKey=true)]
   public CodebookVersion CodebookVersion
   {
      ...
   }

[Table(Name="dbo.tblResponseCodeTable")]
    public partial class CodebookVersion : ...
    ...
   [Association(Name="CodebookVersion_tblResponseCode", Storage="_ResponseCodes", ThisKey="Id", OtherKey="ResponseCodeTableId")]
   public EntitySet<ResponseCode> ResponseCodes
   {
      ...
   }
[表(Name=“dbo.tblResponseCode”)]
公共部分类响应代码:。。。
...
[关联(Name=“CodebookVersion\u tblResponseCode”,Storage=“\u CodebookVersion”,ThisKey=“ResponseCodeTableId”,OtherKey=“Id”,IsForeignKey=true)]
公共代码本版本代码本版本
{
...
}
[表(Name=“dbo.tblresponsecodable”)]
公共部分类CodebookVersion:。。。
...
[关联(Name=“CodebookVersion\u tblResponseCode”,Storage=“\u Respon
[Table(Name="dbo.tblResponseCode")]
public partial class ResponseCode : ...
    ...
   [Association(Name="CodebookVersion_tblResponseCode", Storage="_CodebookVersion", ThisKey="ResponseCodeTableId", OtherKey="Id", IsForeignKey=true)]
   public CodebookVersion CodebookVersion
   {
      ...
   }

[Table(Name="dbo.tblResponseCodeTable")]
    public partial class CodebookVersion : ...
    ...
   [Association(Name="CodebookVersion_tblResponseCode", Storage="_ResponseCodes", ThisKey="Id", OtherKey="ResponseCodeTableId")]
   public EntitySet<ResponseCode> ResponseCodes
   {
      ...
   }
ResponseCode rc = new ResponseCode()
{
   CodebookVersion = codebookVersion,
   SurveyQuestionName = "Q11",
   Code = 3,
   Description = "Yet another code"
};
db.ResponseCodes.InsertOnSubmit(rc);
db.SubmitChanges();
    ResponseDataContext db = new ResponseDataContext(m_ConnectionString);
    CodebookVersion codebookVersion = db.CodebookVersions.Single(cv => cv.VersionTag == m_CodebookVersionTag); 
    ResponseCode rc = new ResponseCode()
    {   
        ResponseCodeTableId = codebookVersion.Id,   
        SurveyQuestionName = "Q11",   
        Code = 3,   
        Description = "Yet another code"
    };
    db.ResponseCodes.InsertOnSubmit(rc);
    db.SubmitChanges();