简体   繁体   English

linqTOsql在运行时返回“指定的转换无效”异常

[英]linqTOsql returning a “specified cast not valid” exception at runtime

I have two linqTOsql entities that has a parent and child relationship, one to many. 我有两个linqTOsql实体,它们具有父子关系,一对多。 I'm running into an issue that when I retrieve the parent record, I am unable to loop through the related records in the child table. 我遇到了一个问题,当我检索父记录时,我无法遍历子表中的相关记录。

This code fails: 此代码失败:

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

The error is: 错误是:

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

I don't understand why the stacktrace is showing int32 . 我不明白为什么stacktrace显示int32 I'm 99% certain that I have been using long and bigint for all my IDs. 我99%肯定我一直在使用longbigint来获取所有ID。 Just to cover all my bases, heres the parent and child model code: 只是为了涵盖我的所有基础,继承父母和孩子的模型代码:

Parent: 家长:

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

What could be causing my cast exception? 什么可能导致我的演员异常?

Edit - adding table definitions 编辑 - 添加表定义

StreamEntry Table: StreamEntry表:

seID bigint notnull seid bigint notnull

seUrl nvarchar(255) notnull seUrl nvarchar(255)notnull

seHeadline nvarchar(255) notnull seHeadline nvarchar(255)notnull

seBody ntext nullable seBody ntext可以为空

seDescription nvarchar(255) nullable seDescription nvarchar(255)可以为空

seKeywords nvarchar(255) nullable seKeywords nvarchar(255)可以为空

seTitle nvarchar(255) nullable seTitle nvarchar(255)可以为空

seOrder bigint notnull seOrder bigint notnull

seDateCreated datetime notnull seDateCreated datetime notnull

seDateModified datetime notnull seDateModified datetime notnull

StreamID bigint notnull StreamID bigint notnull

AllowComents bit notnull AllowComents位不畅

FieldInstance Table: FieldInstance表:

ftID bigint notnull ftID bigint notnull

ftIsRequired bit notnull ftIsRequired bit notnull

ftLabel nvarchar(50) notnull ftLabel nvarchar(50)notnull

ftStrValue nvarchar(1000) nullable ftStrValue nvarchar(1000)可以为空

ftDateTimeValue datetime nullable ftDateTimeValue datetime可以为空

ftIntValue int nullable ftIntValue int nullable

ftDecValue decimal(18,0) nullable ftDecValue十进制(18,0)可以为空

ftOrder bigint notnull ftOrder bigint notnull

ftStreamEntryID bigint notnull --- FK to StreamEntry table ftStreamEntryID bigint notnull --- FK到StreamEntry表

ftFieldTypeID bigint notbull ftFieldTypeID bigint notbull

Edit - Added StreamEntry Record 编辑 - 添加了StreamEntry记录

This code: 这段代码:

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

Returns: 返回:

6 6

asd ASD

asd ASD

> >

> >

> >

> >

0 0

2010-11-16 4:10:45 PM 2010-11-16 4:10:45 PM

2010-11-16 4:10:45 PM 2010-11-16 4:10:45 PM

75 75

False

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM