I cannot fetch a record if I have a Binary field.

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
linusconcepcion
Posts: 0
Joined: Thu 27 Dec 2018 04:17

I cannot fetch a record if I have a Binary field.

Post by linusconcepcion » 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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: I cannot fetch a record if I have a Binary field.

Post by Shalex » Sat 16 Nov 2019 17:31

Varbinary should be mapped to System.Byte[]: https://www.devart.com/linqconnect/docs ... pping.html.

Please change the mapping via the interface of Entity Developer: right-click on *.lqml > Open With > Entity Developer, navigate to Tools > Entity Developer > Model Explorer, open Property Editor for the Content property, set Type=System.Byte[]. Save the model to regenerate the code.

Does this help?

linusconcepcion
Posts: 0
Joined: Thu 27 Dec 2018 04:17

Re: I cannot fetch a record if I have a Binary field.

Post by linusconcepcion » Mon 18 Nov 2019 13:25

Thanks. I can try that, but can I ask what is the purpose for the Devart.Linq.Binary object if it is no longer supported? I was hoping to keep the same implementation that we had for Linq to SQL.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: I cannot fetch a record if I have a Binary field.

Post by Shalex » Fri 22 Nov 2019 15:18

We will add a description of the Devart.Data.Linq.Binary class in our documentation and notify you.

Post Reply