错误SQL CE 4.0存储图像的ASP.NET MVC 3和Entity Framework 4.1 code首先 [英] Error storing Image in SQL CE 4.0 with ASP.NET MVC 3 and Entity Framework 4.1 Code First

查看:271
本文介绍了错误SQL CE 4.0存储图像的ASP.NET MVC 3和Entity Framework 4.1 code首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储/在SQL精简版(CE)数据库中保存图像。

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; }

该数据库与Photo列的图像数据类型创建为可以在这里看到:

The database is created with the image data type for the Photo column as can be seen here:

问题是:

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

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的支持这些数据类型的。在SQL防爆preSS之间的比较和SQL精简版(CE)我们有一个SQL CE支持二进制(BLOB)存储通过使用图像数据类型。

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.

图片 =可变长度的二进制数据
  以2 ^ 30-1的最大长度
  (1,073,741,823)字节。存储是
  长度字节的值。

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?

请注意:

我也试过最大长度数据注释:

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.

编辑:

我发现后了解 EF 4.1的发布。它具有如下:

I found the post about the release of EF 4.1. It has the following:

默认长度的变化对非关键
  串并从'128'二进制列
  到最大。 SQL精简不支持
  '最大'栏目,针对运行时
  SQL精简额外的code首先
  公约将设置一个默认长度
  4000的大约有更多的细节
  包括在最近的博客改变
  后(以下链接)。

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).

好吧好吧好吧...我能得到它的工作的唯一方法是做什么的描述<一个href=\"http://blogs.msdn.com/b/adonet/archive/2011/03/29/ef-4-1-rtw-change-to-default-maxlength-in-$c$c-first.aspx\">here,也就是说,设置 DbContext.Configuration.ValidateOnSaveEnabled = FALSE 。这是一个解决方法,因为后建议。

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.

推荐答案

对于那些遇到此问题,埃里克·詹森Ejlskov发布的这表明了解决方法,以这个bug的工作控制台应用程序。由于OP指出,答案的一个关键部分是:

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:

    public StudentContext()
    {
        // Required to prevent bug - http://stackoverflow.com/questions/5737733
        this.Configuration.ValidateOnSaveEnabled = false;        
    }

有一个更好的解决办法已经找到。不要禁用验证。
[从博客文章的更新]
更新:@DamienGuard,LINQ到SQL和EF code首先成名指出,更好,更提供商无关的解决方案是使用最大长度,而不是类型名=NTEXT

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".

更新2:使用[MAXLENGTH] prevents任何验证错误,并禁用验证,不需要

这篇关于错误SQL CE 4.0存储图像的ASP.NET MVC 3和Entity Framework 4.1 code首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆