[英]linqTOsql returning a “specified cast not valid” exception at runtime
我有兩個linqTOsql實體,它們具有父子關系,一對多。 我遇到了一個問題,當我檢索父記錄時,我無法遍歷子表中的相關記錄。
此代碼失敗:
public string test()
{
string output;
StreamEntry entry = genesisRepository.StreamEntries.FirstOrDefault(x => x.seID == 6);
output = entry.seUrl.ToString() + "<br />";
foreach(var item in entry.FieldInstance)
{
output = "<ul>";
output += "<li>" + item.fiLabel.ToString() + "</li>";
output = "</ul>";
}
return output;
}
錯誤是:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 230:
Line 231: output = entry.seUrl.ToString() + "<br />";
Line 232: foreach(var item in entry.FieldInstance)
Line 233: {
Line 234:
Source File: C:\pathtoscript.cs Line: 232
Stack Trace:
[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Int32() +5002837
System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) +38
Read_FieldInstance(ObjectMaterializer`1 ) +1993
System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +32
System.Data.Linq.EntitySet`1.Load() +124
System.Data.Linq.EntitySet`1.GetEnumerator() +13
我不明白為什么stacktrace顯示int32
。 我99%肯定我一直在使用long
和bigint
來獲取所有ID。 只是為了涵蓋我的所有基礎,繼承父母和孩子的模型代碼:
家長:
[Table]
public class StreamEntry
{
[HiddenInput(DisplayValue = false)]
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public long seID { get; set; }
/* other fields removed for brevity */
// relationship (one entry to many FieldInstances)
// uses EntitySet<FieldInstance> and OtherKey for the FK in FieldInstance
// which is the "Other" table.
private EntitySet<FieldInstance> _FieldInstance = new EntitySet<FieldInstance>();
[System.Data.Linq.Mapping.Association(Storage = "_FieldInstance", OtherKey = "fiStreamEntryID")]
public EntitySet<FieldInstance> FieldInstance
{
get { return this._FieldInstance; }
set { this._FieldInstance.Assign(value); }
}
}
兒童:
[Table]
public class FieldInstance
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public long fiID { get; set; }
/* Other field removed for brevity */
[Column]
public long fiStreamEntryID { get; set; } // FK
// Relationship (many FieldInstances to one StreamEntry)
// using EntityRef<StreamEntry> and ThisKey
// which is "This" table's FK
private EntityRef<StreamEntry> _StreamEntry;
[System.Data.Linq.Mapping.Association(Storage = "_StreamEntry", ThisKey = "fiStreamEntryID")]
public StreamEntry StreamEntry
{
get { return this._StreamEntry.Entity; }
set { this._StreamEntry.Entity = value; }
}
}
什么可能導致我的演員異常?
StreamEntry表:
seid bigint notnull
seUrl nvarchar(255)notnull
seHeadline nvarchar(255)notnull
seBody ntext可以為空
seDescription nvarchar(255)可以為空
seKeywords nvarchar(255)可以為空
seTitle nvarchar(255)可以為空
seOrder bigint notnull
seDateCreated datetime notnull
seDateModified datetime notnull
StreamID bigint notnull
AllowComents位不暢
FieldInstance表:
ftID bigint notnull
ftIsRequired bit notnull
ftLabel nvarchar(50)notnull
ftStrValue nvarchar(1000)可以為空
ftDateTimeValue datetime可以為空
ftIntValue int nullable
ftDecValue十進制(18,0)可以為空
ftOrder bigint notnull
ftStreamEntryID bigint notnull --- FK到StreamEntry表
ftFieldTypeID bigint notbull
這段代碼:
public string test()
{
string output;
StreamEntry entry = genesisRepository.StreamEntries.FirstOrDefault(x => x.seID == 6);
output = entry.seID.ToString() + "<br />";
output += entry.seUrl + "<br />";
output += entry.seHeadline + "<br />";
output += entry.seBody + "<br />";
output += entry.seDescription + "<br />";
output += entry.seKeywords + "<br />";
output += entry.seTitle + "<br />";
output += entry.seOrder.ToString() + "<br />";
output += entry.seDateCreated.ToString() + "<br />";
output += entry.seDateModified.ToString() + "<br />";
output += entry.StreamID.ToString() + "<br />";
output += entry.AllowComments.ToString() + "<br />";
return output;
}
返回:
6
ASD
ASD
>
>
>
>
0
2010-11-16 4:10:45 PM
2010-11-16 4:10:45 PM
75
假
是否有可能在不更新DBML的情況下更新基礎數據庫中的列類型?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.