简体   繁体   English

Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。”

[英]Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.”

This is the first time I have tried to use LINQ. 这是我第一次尝试使用LINQ。 I have a database table that consists of two string columns, one bit column, and an id column defined as an int. 我有一个数据库表,该表由两个字符串列,一位列和一个定义为int的id列组成。 The table holds configuration data so there is only a single row. 该表保存配置数据,因此只有一行。

Database definition... 数据库定义...

CREATE TABLE [dbo].[Configuration](
[Id] [int] IDENTITY(1,1) NOT NULL,
[LegalRepository] [nvarchar](100) NOT NULL,
[TitleRepository] [nvarchar](100) NOT NULL,
[AlwaysOpenOnDesktop] [bit] NOT NULL
) ON [PRIMARY]

The query looks like this... 查询看起来像这样...

Configuration config = fileSearchDB.Configurations.Single(c => c.Id == configId);

The Configuration class is... Configuration类是...

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Configuration")]
public partial class Configuration
{

    private int _Id;

    private System.Data.Linq.Binary _LegalRepository;

    private System.Data.Linq.Binary _TitleRepository;

    private bool _AlwaysOpenOnDesktop;

    public Configuration()
    {
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.Always, DbType="Int NOT NULL IDENTITY", IsDbGenerated=true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this._Id = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LegalRepository", DbType="VarBinary(MAX) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
    public System.Data.Linq.Binary LegalRepository
    {
        get
        {
            return this._LegalRepository;
        }
        set
        {
            if ((this._LegalRepository != value))
            {
                this._LegalRepository = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TitleRepository", DbType="VarBinary(MAX) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
    public System.Data.Linq.Binary TitleRepository
    {
        get
        {
            return this._TitleRepository;
        }
        set
        {
            if ((this._TitleRepository != value))
            {
                this._TitleRepository = value;
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AlwaysOpenOnDesktop", DbType="Bit NOT NULL")]
    public bool AlwaysOpenOnDesktop
    {
        get
        {
            return this._AlwaysOpenOnDesktop;
        }
        set
        {
            if ((this._AlwaysOpenOnDesktop != value))
            {
                this._AlwaysOpenOnDesktop = value;
            }
        }
    }
}

The int field configId = 1, which is the id of the only row in the table. int字段configId = 1,它是表中唯一行的ID。

Why am I receiving this error? 为什么会收到此错误?

Unable to cast object of type 'System.String' to type 'System.Byte[]' 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象

Thanks, Gary 谢谢,加里

UPDATED: I have added the definition of the Configuration class and that the configId variable is an int 更新:我添加了Configuration类的定义,并且configId变量是一个int

After looking at the definition of the Configuration class I see that the repository fields are defined as binary. 查看Configuration类的定义后,我看到存储库字段被定义为二进制。 They should be strings. 它们应该是字符串。 I deleted the configuration table and readded it and the columns were now defined as strings. 我删除了配置表并读取了它,现在将列定义为字符串。

Your definition of Configuration declares LegalRepository and TitleRepository as immutable binary fields, when in the database, they're simple strings. 您对Configuration的定义将LegalRepository和TitleRepository声明为不可变的二进制字段,当它们在数据库中时,它们是简单的字符串。 Either the DB declaration of the fields must change to varbinary , or the object fields should become ordinary string properties. 字段的DB声明必须更改为varbinary ,或者对象字段应成为普通的string属性。

暂无
暂无

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

相关问题 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误 - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) 无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.String[]' 无法将“System.Byte”类型的 object 转换为“System.String”类型。 - Unable to cast object of type 'System.Byte' to type 'System.String'.' 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 无法将'System.Data.Linq.Binary'类型的对象强制转换为'System.Byte []' - Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]' 无法将System.string强制转换为System.Byte [] - Unable to cast System.string to System.Byte[] 错误:System.InvalidCastException:无法将“System.Byte”类型的对象转换为“System.Int32”类型 - Error: System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM