使用 ASP.NET MVC 3 和 Entity Framework 4.1 Code First 在 SQL CE 4.0 中存储图像时出错
我正在尝试在 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 支持这些 数据类型。在 SQL 之间的比较中Express 和 SQL Compact Edition (CE) 我们拥有 SQL CE 通过使用图像数据类型支持二进制 (BLOB) 存储。
图像 = 可变长度二进制数据 最大长度为 2^30–1 (1,073,741,823) 字节。存储是 值的长度(以字节为单位)。
我认为图像应该可以完成任务。
我在这里做错了什么?这是一个错误吗?
注意:
我还尝试了 MaxLength 数据注释:
[Column(TypeName = "image")]
[MaxLength(int.MaxValue)]
public byte[] Photo { get; set; }
但我得到了这个异常:
Binary column with MaxLength greater than 8000 is not supported.
编辑:
我找到了 帖子 有关 EF 4.1 的发布。它具有以下特点:
更改非密钥的默认长度 来自“128”的字符串和二进制列 到“最大”。 SQL Compact 不支持 'Max' 列,当运行时 SQL Compact 额外的 Code First 约定将设置默认长度 4000。还有更多详细信息 最近博客中包含的更改 发布(下面的链接)。
好吧好吧...我能让它工作的唯一方法就是按照描述的操作此处,即设置DbContext.Configuration.ValidateOnSaveEnabled = false
。正如帖子所建议的,这是一种解决方法。
I'm trying to store/save an image in an SQL Compact Edition (CE) database.
I declare the field in my Student model as:
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
The database is created with the image data type for the Photo column as can be seen here:
The problem is:
When I run the app and try to save a Student with a Photo of 3 MB (for example), I get an exception:
validationError.ErrorMessage = "The field Photo must be a string or array type
with a maximum length of '4000'."
SQL Server CE supports these Data Types. In this comparison between SQL Express and SQL Compact Edition (CE) we have that SQL CE supports Binary (BLOB) storage through the use of image data type.
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.
Image should do the job I think.
What am I doing wrong here? Is this a bug?
Note:
I also tried the MaxLength data annotation:
[Column(TypeName = "image")]
[MaxLength(int.MaxValue)]
public byte[] Photo { get; set; }
but I get this exception:
Binary column with MaxLength greater than 8000 is not supported.
Edit:
I found the post about the release of EF 4.1. It has the following:
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).
Well well well... the only way I could get it working was doing what is described here, that is, setting DbContext.Configuration.ValidateOnSaveEnabled = false
. This is a workaround as the post suggests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于遇到此问题的用户,Erik Ejlskov Jensen 发布了 a工作控制台应用程序,它演示了此错误的解决方法。正如OP指出的,答案的关键部分是:
已经找到了更好的解决方案。不要禁用验证。
[博客文章更新]
更新:以 LINQ to SQL 和 EF Code First 闻名的 @DamienGuard 指出,更好、更与提供商无关的解决方案是使用 MaxLength 而不是 TypeName = “ntext”。
更新 2:使用 [MaxLength] 可防止任何验证错误,并且不需要禁用验证。
For those experiencing this problem, Erik Ejlskov Jensen posted a working console application which demonstrates the workaround to this bug. As the OP noted, a key part of the answer is:
A better solution has been found. Do not disable validation.
[Updates from blog post]
UPDATE: @DamienGuard, of LINQ to SQL and EF Code First fame, pointed out that a better and more provider agnostic solution is to use MaxLength rather than TypeName = “ntext”.
UPDATE 2: Using [MaxLength] prevents any validation errors, and disabling validation is not required.
使用 MaxLength 数据注释指定不最大长度的方法是不提供最大值。例如:
SQL Compact 提供程序随后会将属性映射到“image”,并且 EF 验证将识别出没有指定最大长度,因此不需要禁用。如果您想明确映射到“图像”列,那么您可以这样做:
这将在使用 SQL Compact 时产生相同的结果。
The way to specify no maximum length using the MaxLength data annotation is to provide no maximum value. For example:
The SQL Compact provider will then map the property to "image" and EF validation will recognize that there is no max length specified and so does not need to be disabled. If you want to be explicit about mapping to an "image" column then you can do this:
which will produce the same result when using SQL Compact.
我知道这已经太晚了,但它可以使其他程序员受益。
您可以添加到 OnModelCreating(DbModelBuilder modelBuilder) 方法内的 context 类
I know this is too late, but it could benefit other programmers.
You could add to the context class inside the OnModelCreating(DbModelBuilder modelBuilder) method