c# – 使用Entity Framework Code-First方法在SQL CE 4中存储文件

我正在尝试将实体添加到SQL CE 4,它具有byte []类型的属性.从msdn我发现只有图像类型可以容纳大文件(在我的情况下,它们不是那么大,但仍然超过二进制类型8000字节的限制).
这是模型:
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;
    }
}

我也有从DbContext派生的类,当我使用它时这样的东西

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

错误:

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’.”

我试图在属性上使用MaxLength属性,并将错误更改为“不支持长度超过8000的二进制clumn”.

似乎EF将列映射到二进制类型而不是图像.如何解决这个问题?

解决方法

请参阅此博客文章: http://erikej.blogspot.com/2011/04/saving-images-to-sql-server-compact.html – 您需要禁用验证作为解决方法.

相关文章

1:最直白的循环遍历方法,可以分为遍历key--value键值对以及...
private void ClearTextBox(){ foreach (var control in pnl...
原文叫看《墨攻》理解IOC概念 2006年多部贺岁大片以让人应接...
右击文件夹->安全选项卡->添加->高级-...