Store file in SQL CE 4 using the Entity Framework Code-First approach

I am trying to add an entity in SQL CE 4 that have a property of type byte []. From msdn, I found out that only the image type can contain large files (in my case, they are not so large, but still exceed the binary type of 8000 bytes). Here is the model:

public class TabModel { [Key] public Guid Id { get; set; } public string Title { get; set; } public string Subtitle { get; set; } public string Artist { get; set; } public string Album { get; set; } public string Author { get; set; } public string TabAuthor { get; set; } public DateTime DateAdded { get; set; } [Column("file",TypeName="image")] public byte[] File { get; set; } public TabModel() { Id = Guid.NewGuid(); DateAdded = DateTime.Now; } } 

I also have a class derived from DbContext, and when I use it something like this

 library.Tabs.Add(tab);//tab of type TabModel, File is array with length equals 12000 library.SaveChanges();//throws exception 

Mistake:

Verification failed for one or more objects. See the EntityValidationErrors Property for more information. EntityValidationErrors [0] = "The field file must be a string or an array of type with a maximum length of" 4000 ".

I tried to use the MaxLength attribute in the properties and error changes in "Binary clumn with a length greater than 8000 is not supported."

It seems that EF maps the column to a binary type, not an image. How to fix it?

+7
source share
1 answer

See this blog post: http://erikej.blogspot.com/2011/04/saving-images-to-sql-server-compact.html - you need to disable validation as a workaround.

+7
source

All Articles