【问题标题】:Store file in SQL CE 4 using Entity Framework Code-First approach使用实体框架代码优先方法在 SQL CE 4 中存储文件
【发布时间】:2011-10-03 18:06:30
【问题描述】:

我正在尝试将实体添加到具有 byte[] 类型属性的 SQL CE 4。从 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

错误:

一个或多个实体的验证失败。看 'EntityValidationErrors' 属性以获取更多详细信息。 EntityValidationErrors[0]="字段文件必须是字符串或数组 最大长度为 '4000' 的类型。"

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

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

【问题讨论】:

    标签: c# sql-server-ce entity-framework-4.1 ef-code-first


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      相关资源
      最近更新 更多