I cannot fetch a record if I have a Binary field.
Posted: Fri 15 Nov 2019 05:01
This is my scenario. Assume I have this table in SQL Server
Files
---------
FileID int (PK)
Description varchar(50) not null
Content varbinary(max)
So, my C# class looks something like this:
[Table(Name="dbo.Files")]
public class File
{
[Column(Name = "FileID", DbType = "int NOT NULL IDENTITY", IsPrimaryKey="true")]
public int FileID { get {..} set {…} }
[Column(Name="Description", DbType = "varchar(max)")]
public string Description { get { … } set { … } }
[Column(Name="Content", DbType="varbinary(max)")]
public Devart.Data.Linq.Binary Content { get { … } set { … } }
}
When I run this code:
using (var context = CreateDataContext()) {
var file = context.Files.Where(f => f.FileID==100).SingleOrDefault();
Console.WriteLine(file.Description);
}
The code crashes even before the Console.WriteLine(). It crashes on the SingleOrDefault. The exception being returned is:
Unable to cast object of type 'Devart.Data.Linq.Engine.DeferredEnumerable`1[Origami.Models.File]' to type 'System.Collections.Generic.IEnumerable`1[Devart.Data.Linq.Binary]'
Can someone help on what is the proper way to fetch a record with a Binary field? The code above works fine in Linq to SQL.
Files
---------
FileID int (PK)
Description varchar(50) not null
Content varbinary(max)
So, my C# class looks something like this:
[Table(Name="dbo.Files")]
public class File
{
[Column(Name = "FileID", DbType = "int NOT NULL IDENTITY", IsPrimaryKey="true")]
public int FileID { get {..} set {…} }
[Column(Name="Description", DbType = "varchar(max)")]
public string Description { get { … } set { … } }
[Column(Name="Content", DbType="varbinary(max)")]
public Devart.Data.Linq.Binary Content { get { … } set { … } }
}
When I run this code:
using (var context = CreateDataContext()) {
var file = context.Files.Where(f => f.FileID==100).SingleOrDefault();
Console.WriteLine(file.Description);
}
The code crashes even before the Console.WriteLine(). It crashes on the SingleOrDefault. The exception being returned is:
Unable to cast object of type 'Devart.Data.Linq.Engine.DeferredEnumerable`1[Origami.Models.File]' to type 'System.Collections.Generic.IEnumerable`1[Devart.Data.Linq.Binary]'
Can someone help on what is the proper way to fetch a record with a Binary field? The code above works fine in Linq to SQL.