gpt4 book ai didi

c# - linqTOsql 在运行时返回 "specified cast not valid"异常

转载 作者:行者123 更新时间:2023-11-30 16:32:15 24 4
gpt4 key购买 nike

我有两个具有父子关系的 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

我不明白为什么堆栈跟踪显示 int32。我 99% 确定我一直在为我的所有 ID 使用 longbigint。为了涵盖我的所有基础,这里是父子模型代码:

家长:

[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); }
}

}

child :

[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 Table:

seID bigint notnull

seUrl nvarchar(255) notnull

seHeadline nvarchar(255) notnull

seBody ntext nullable

seDescription nvarchar(255) nullable

seKeywords nvarchar(255) nullable

seTitle nvarchar(255) nullable

seOrder bigint notnull

seDateCreated datetime notnull

seDateModified datetime notnull

StreamID bigint notnull

AllowComents bit notnull

字段实例表:

ftID bigint notnull

ftIsRequired bit notnull

ftLabel nvarchar(50) notnull

ftStrValue nvarchar(1000) nullable

ftDateTimeValue datetime nullable

ftIntValue int nullable

ftDecValue decimal(18,0) nullable

ftOrder bigint notnull

ftStreamEntryID bigint notnull --- FK to StreamEntry table

ftFieldTypeID bigint notbull

编辑 - 添加 StreamEntry 记录

这段代码:

   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

False

最佳答案

是否可以在不更新 DBML 的情况下更新基础数据库中的列类型?

关于c# - linqTOsql 在运行时返回 "specified cast not valid"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com