简体   繁体   中英

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. I have a database table that consists of two string columns, one bit column, and an id column defined as an int. 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...

[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.

Why am I receiving this error?

Unable to cast object of type 'System.String' to type 'System.Byte[]'

Thanks, Gary

UPDATED: I have added the definition of the Configuration class and that the configId variable is an int

After looking at the definition of the Configuration class I see that the repository fields are defined as binary. 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. Either the DB declaration of the fields must change to varbinary , or the object fields should become ordinary string properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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