Search code examples
c#sqllinqvarbinary

Can't insert NULL into varbinary column using LINQ


I have code that updates a record via LINQ to SQL:

dc.ClCxDemographicFileAttachments.InsertOnSubmit(new MasterDataContext.ClCxDemographicFileAttachment()
{
    ...
    FileData = characteristicDataFileAttachment.FileData,
});

The FileData column in the ClCxDemographicFileAttachment table is VARBINARY(MAX) and nullable in the SQL table and the column/property in the DataContext is System.Data.Linq.Binary.

The problem is that if I set the FileData property to null, LINQ generates an empty byte[] for that column (0x) instead of NULL. I want the latter. How do I fix this?


Solution

  • You should just use a data type of byte[] instead of System.Data.Linq.Binary otherwise some implicit conversion will take place and you will not get a null entry in your database.