Tom Styles
Tom Styles

Reputation: 1082

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.

Upvotes: 11

Views: 9762

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

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

Upvotes: 21

Related Questions