Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF4 Mapping varbinary(max) to Binary - Code First Error

I have a POCO class called Attachment that maps to a table in SqlServer with a VarBinary(max) field in it. The field contains files.

The POCO class looks like this

public class Attachment
{
    public string AttachmentId { get; set; }
    public string AttachmentTypeId { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public Binary Data { get; set; }
}

The Mapping looks like this

modelBuilder.Entity<Attachment>().Property(a => a.Data).HasColumnName("col_data");

However the mapping is throwing an error

The type 'System.Date.Linq.Binary' must be a non-nullable value type in order to use it as a parameter 'T'

The mapping works fine if I use a byte array but this seems to be corrupting the data on the way through.

The file in the database has an initial binary string like:-

0x504B0304140008000800027923400000000000000000000000001F000000

I think this is a JPG file. Any help getting the file out of the DB in one piece would be appreciated.

like image 536
Tom Styles Avatar asked Jan 06 '12 11:01

Tom Styles


1 Answers

Binary data type is only for Linq-to-Sql. EF is not able to work with it. The correct data type for EF is byte array (byte[]).

like image 94
Ladislav Mrnka Avatar answered Nov 18 '22 22:11

Ladislav Mrnka