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

使用ASP.NET MVC 3和实体框架4.1代码首先在SQL CE 4.0中存储图像

发布时间:2020-12-16 00:40:16 所属栏目:asp.Net 来源:网络整理
导读:我试图在SQL Compact Edition(CE)数据库中存储/保存图像。 我在我的学生模型中声明为: [Column(TypeName = "image")]public byte[] Photo { get; set; } 数据库创建的照片列的图像数据类型可以在这里看到: 问题是: 当我运行应用程序,并尝试保存一张3 MB
我试图在SQL Compact Edition(CE)数据库中存储/保存图像。

我在我的学生模型中声明为:

[Column(TypeName = "image")]
public byte[] Photo { get; set; }

数据库创建的照片列的图像数据类型可以在这里看到:

问题是:

当我运行应用程序,并尝试保存一张3 MB照片的学生(例如),我得到一个例外:

validationError.ErrorMessage = "The field Photo must be a string or array type
with a maximum length of '4000'."

SQL Server CE支持这些Data Types.在SQL Express和SQL Compact Edition(CE)之间的这个comparison中,我们通过使用图像数据类型支持SQL CE支持二进制(BLOB)存储。

Image = Variable-length binary data
with a maximum length of 2^30–1
(1,073,741,823) bytes. Storage is the
length of the value in bytes.

形象应该做我想的工作。

我在这里做错了什么?这是一个bug吗?

注意:

我也尝试过MaxLength数据注释:

[Column(TypeName = "image")]
[MaxLength(int.MaxValue)]
public byte[] Photo { get; set; }

但我得到这个例外:

Binary column with MaxLength greater than 8000 is not supported.

编辑:

我发现post发布了EF 4.1。它有以下:

Change of default length for non-key
string and binary columns from ‘128’
to ‘Max’. SQL Compact does not support
‘Max’ columns,when running against
SQL Compact an additional Code First
convention will set a default length
of 4000. There are more details about
the change included in a recent blog
post (link below).

好吧,我唯一可以让它工作的方式是做所描述的here,也就是设置DbContext.Configuration.ValidateOnSaveEnabled = false。这是一个解决方法,因为帖子建议。

解决方法

对于那些遇到这个问题的人来说,Erik Ejlskov Jensen发布了 a working console application,这显示了这个bug的解决方法。正如OP所说,答案的关键部分是:
public StudentContext()
    {
        // Required to prevent bug - http://stackoverflow.com/questions/5737733
        this.Configuration.ValidateOnSaveEnabled = false;        
    }

已经找到了更好的解决方案。不要禁用验证。
[博客文章更新]
更新:LINQ to SQL和EF Code First fame的@DamienGuard指出,一个更好和更多的提供商不可知的解决方案是使用MaxLength而不是TypeName =“ntext”。

更新2:使用[MaxLength]可防止任何验证错误,并且不需要禁用验证。

(编辑:李大同)

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

    推荐文章
      热点阅读