【发布时间】:2013-08-06 23:19:49
【问题描述】:
我正在使用 Linq to SQL 访问我的 SQL Ce 数据库。
var Logcontext = new LogContext(GCUtility.LconnectionPool.Connection);
{
var _ApplicationsK = (from u in Logcontext.Applications select u.PK_Key).ToList<int>();
}
在上面的代码中,PK_Key 是数据库中一个自动递增的整数变量。它抛出一个异常“System.Data.SqlServerCe.dll 中发生'System.StackOverflowException' 类型的未处理异常”。我尝试在 Visual Studio 上清理、重建、重新启动等。我正在使用 Linq 运行时版本 v4.0.30319。我的代码有什么问题?
表结构如下。
PK_Key (Type = int, PrimaryKey = true, Identity = True, Identity increment = 1, seed =1 )
Username (Type = varchar, AllowNulls = True)
Linq SqlMetal.exe 为列 PK_Key 生成以下代码
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_PK_Key", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int PK_Key
{
get
{
return this._PK_Key;
}
set
{
if ((this._PK_Key != value))
{
this.OnPK_KeyChanging(value);
this.SendPropertyChanging();
this._PK_Key = value;
this.SendPropertyChanged("PK_Key");
this.OnPK_KeyChanged();
}
}
}
【问题讨论】:
-
表格有多少行?另外,您可以发布包含查询的其余方法吗?
_ApplicationsK应该分配在堆上,因此无论它的大小如何,它都不会导致 SOException。更有可能是您无意中在堆栈上放置了一个非常大的结构。 -
当您遇到堆栈溢出异常时,查找问题的好地方是堆栈跟踪。
-
插入工作没有任何问题。到目前为止,我只测试了 30 行以下。
-
@Jehonathan 您可能无意中在事件处理中结束递归,用递归调用填充堆栈。
-
我从来没有在我的逻辑中递归过任何地方。我遵循了执行路径,它在调用 ToList() 时抛出了异常。 stackoverflow.com/questions/1027628/… 有一个指向 Linq 3.5 错误的相关问题。他们在那里说“当使用'自动生成值'= True时,你必须将'延迟加载'设置为False - 否则你会得到递归错误。”但我不知道我该怎么做,需要在哪里设置“延迟加载”?还有在哪里可以看到 Visual Studio 中的堆栈跟踪?