ePezhman
ePezhman

Reputation: 4010

dbml changes the type of varbinay(max) on Sql Server to Systm.Data.Linq.Binary but not byte[]

I have some table in Sql Server which has VarBinary(MAX) and I want to upload files to them, I need to make the Dbml to make the field byte[] but instead I get Systm.Data.Linq.Binary.

Why is that and how to make the default as byte[]? Thanks.

I read the file in my MVC 3 coroller action like this (resourceFile is a HttpPostedFileBase and newFile has the byte[])

newFile.FileContent = new byte[resourceFile.ContentLength];
resourceFile.InputStream.Read(newFile.FileContent, 0, resourceFile.ContentLength);

Upvotes: 0

Views: 635

Answers (2)

user1429080
user1429080

Reputation: 9166

The System.Linq.Data.Binary type is a wrapper for a byte array that makes the wrapped byte array immutable. See msdn page.

Sample code to illustrate immutability:

byte[] goesIn = new byte[] { 0xff };
byte[] comesOut1 = null;
byte[] comesOut2 = null;
byte[] comesOut3 = null;

System.Data.Linq.Binary theBinary = goesIn;

comesOut1 = theBinary.ToArray();
comesOut1[0] = 0xfe;

comesOut2 = theBinary.ToArray();

theBinary = comesOut1;

comesOut3 = theBinary.ToArray();

The immutability can be seen after changing the value of the first byte in comesOut1. The byte[] wrapped in theBinary is not changed. Only after you assign the whole byte[] to theBinary does it change.

Anyway for your purpose you can use the Binary field. To assign a new value to it do as Dennis wrote in his answer. To get the byte array out of it, use the .ToArray() method.

Upvotes: 1

Dennis
Dennis

Reputation: 37760

You can use Binary to store byte[] as well, because Binary has implicit conversion from byte[]:

myEntity.MyBinaryProperty = File.ReadAllBytes("foo.txt");

Upvotes: 1

Related Questions