加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:47:31 所属栏目:百科 来源:网络整理
导读:我正在尝试将实体添加到SQL CE 4,它具有byte []类型的属性.从msdn我发现只有图像类型可以容纳大文件(在我的情况下,它们不是那么大,但仍然超过二进制类型8000字节的限制). 这是模型: public class TabModel{ [Key] public Guid Id { get; set; } public stri
我正在尝试将实体添加到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 – 您需要禁用验证作为解决方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读