linq 到 sql + 查询对象时出现 stackoverflow 异常

发布于 2024-07-25 02:27:49 字数 787 浏览 3 评论 0原文

好的,当我尝试查询主键(如果实体中的主键设置为“自动生成值”)时,我已经确认只有这个问题 - 但如果没有这个,我该如何插入? 抱歉,如果这是一个 linq2sql 菜鸟,但我刚刚开始使用它。

如何在关闭此选项的情况下使用 Linq to Sql,同时让数据库处理 pk? 我不想每次都必须 qry 才能获得我应该分配的 pk...

我希望有人可以帮助我,我完全无法在我的一个项目中使用 linq to sql,不太确定该怎么做...这里是一个示例,该行抛出 StackOverflow 异常。

MyDataContext dc = new MyDataContext(ConnStr);
var obj = dc.MyDataTable.AsQueryable().SingleOrDefault(a => a.pkID == 4);

-- 第二行抛出 StackOverflow 异常。

这是使用相同数据上下文的另一个示例

var o = dc.MyDataTable.Take(1); <-- works fine
var abc = o.ToArray();  <-- unable to evaluate, debugger stops

我可以尝试什么想法吗? 我似乎可以在同一解决方案的另一个项目中使用 linq to sql 。

- 更新 - 我忘了提及这个特定实体“MyDataTable”的 pk 设置为“自动生成值”——我将其设置为这个,因为我有 sql 执行自动增量,这是标识列。

OK, i have confirmed i only this issue when i attempt to query on the primary key if that primary key in the entity is set to 'Auto Generated Value' -- but w/o this, how can i insert? Sorry if this is a noob linq2sql but i just started working with it.

How does one go about using Linq to Sql with this option turned off but also have the db handle the pk's? I would hate to have to qry everytime to get the pk i should assign...

I hope someone can help me out, im completely unable to use linq to sql in one of my projects, not really sure what to do... here is an example, this line throws a StackOverflow exception.

MyDataContext dc = new MyDataContext(ConnStr);
var obj = dc.MyDataTable.AsQueryable().SingleOrDefault(a => a.pkID == 4);

-- That second line throws the StackOverflow exception.

Heres another example using the same datacontext

var o = dc.MyDataTable.Take(1); <-- works fine
var abc = o.ToArray();  <-- unable to evaluate, debugger stops

Any ideas what i can try? I seem to be fine using linq to sql in another project in the same solution.

-- UPDATE--
I forgot to mention that this particular entity 'MyDataTable' has the pk set as 'Auto Generated Value' -- i have it set to this cause i have sql doing auto increment and this is the identity column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戴着白色围巾的女孩 2024-08-01 02:27:49

pkID是如何实现的? 它有可能以某种方式递归吗?

How is pkID implemented? Any chance it's recursive in some way?

东风软 2024-08-01 02:27:49

Take(1) 的工作并不令我惊讶,因为它并没有真正执行任何东西(它被推迟到数据被迭代为止)。

这是一个有趣的问题 - 尤其是因为 SingleOrDefault(x=>x.ID == id) 实际上有 内部进行不同的处理 - 它将其识别为主键查找并首先检查身份管理器。

编辑作为一个古怪的事情,尝试 .Where(x=>x.ID == id).SingleOrDefault() - 根据错误(上一个链接),在 4.0 发布之前不会使用身份查找技巧。

我首先想知道:

  • ID getter/setter 中有什么奇怪的东西吗(你添加了代码吗?)
    • 您在该类型的分部类中做过任何事情吗?
  • 它是继承链的一部分吗?
    • 如果是这样,您是否为父类型设置了分部类?
  • 当它爆炸时,你在调用堆栈窗口中得到任何东西吗?

The Take(1) working doesn't surprise me, as this doesn't really execute anything (it is deferred until the data is iterated).

That is an interesting problem - not least because the SingleOrDefault(x=>x.ID == id) actually has different processing internally - it recognises this as a primary-key lookup and checks the identity manager first.

EDIT As an off-the-wall thing, try .Where(x=>x.ID == id).SingleOrDefault() - as per the bug (previous link), this doesn't use the identity lookup trick until 4.0 ships.

I would be start by wondering:

  • is there anything odd in the ID getter/setter (have you added code?)
    • have you done anything in a partial class for this type?
  • is it part of an inheritance chain?
    • and if so, have you monkeyed with a partial class for the parent type?
  • do you get anything in the call-stack window when it explodes?
满地尘埃落定 2024-08-01 02:27:49

这是 LINQ 4.0 中更正的错误

http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40

查询稳定性 Contains 现在可以检测自引用 IQueryable 并且不会导致堆栈溢出

在 .NET 3.5 中解决该问题:当使用“自动生成值”= True 时,则必须将“延迟加载”设置为 False - 否则您会得到递归错误。

That was a bug corrected in LINQ 4.0

http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40

Query stability Contains now detects self-referencing IQueryable and doesn't cause a stack overflow

In .NET 3.5 to solve the problem: When using 'Auto Generated Value' = True, then you must set 'Delay Loaded' to False - otherwise you get the recursion error.

维持三分热 2024-08-01 02:27:49

你的数据表太大了!

编辑。 MyDataTable 真的是 DataTable 吗? 或者它实际上是一个 LINQ to SQL 表<...> ? 如果是这样,请删除 AsQueryable()。

Your datatable is too big!

Edit. Is MyDataTable really a DataTable? Or is it actually a LINQ to SQL Table<...> ? If so, remove the AsQueryable().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文