2

My Code-First entity column is declared like this:

[Required, DataType("image")]
public byte[] Picture { get; set; }

Validations are turned off:

Configuration.ValidateOnSaveEnabled = false;

And is column type is still varbinary(4000).

When I try to save data, Entity Framework throws an exception:

Byte array truncation to a length of 4000.

How to declare that C# byte[] type should be mapped to image SQL type?

Versions:

SQL Server Compact 4

Entity Framework 5 RC

.NET Framework 4.0

Václav Dajbych
  • 2,584
  • 2
  • 30
  • 45
  • 1
    See my blog post here: http://erikej.blogspot.dk/2011/04/saving-images-to-sql-server-compact.html The attribute should be: [Column(TypeName = "image")] public byte[] Photo { get; set; } – ErikEJ Jun 30 '12 at 07:52
  • I know that this is possible in EF4, but there is no `System.ComponentModel.DataAnnotations.ColumnAttribute` in *EntityFramework.dll* version 5 RC. – Václav Dajbych Jul 02 '12 at 19:02
  • 2
    It is in System.ComponentModel.DataAnnotations.Schema namespace (moved there) – ErikEJ Jul 03 '12 at 13:04

1 Answers1

1

Use this attribute:

[MaxLength]
ErikEJ
  • 40,951
  • 5
  • 75
  • 115