Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to add entity to SQL CE 4 which have property of type byte[]. From msdn i have figured out that only image type can hold big files(in my case they not so big, but still over limit of binary type 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 class that 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

Error:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. EntityValidationErrors[0]="The field File must be a string or array type with a maximum length of '4000'."

I have tried to use MaxLength attribute on property and error changes to "Binary clumn with length more 8000 not supported".

It seems like EF maps column to binary type not image. How to fix this?

like image 997
Alexander Avatar asked Jul 12 '11 15:07

Alexander


1 Answers

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.

like image 96
ErikEJ Avatar answered Dec 01 '22 02:12

ErikEJ