BLOB Image Properties in Entity Framework

You can declare image properties as a byte array property, or as a reference properties of the MediaDataObject type (available in the Business Class Library).

Image as a Byte Array

The example below illustrates how to implement image properties in an Entity Framework Code-First class. Declare a byte array property and apply the ImageEditorAttribute to it. Optionally, you can customize the behavior of the image editor using the attribute's parameters.

using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
// ...
[DefaultClassOptions]
public class Employee {
    // ...
    [ImageEditor(ListViewImageEditorMode = ImageEditorMode.PopupPictureEdit,
        DetailViewImageEditorMode = ImageEditorMode.DropDownPictureEdit)]
    public byte[] Photo { get; set; }
}

Refer to the ImageEditorAttribute topic for details on parameters passed to the ImageEditor attribute in the code above. You can also specify image options using the following properties in the Model Editor:

Tip

When an application displays a lot of large images in a List View, it may consume a lot of memory. It is most true for ASP.NET applications with many simultaneously connected clients. To improve the performance, you can use lazy loading, as demonstrated in the How to enable delayed loading of images in Entity Framework example.

Image as a MediaDataObject

The example below illustrates how to implement image properties of the MediaDataObject type (available in the Business Class Library) in an Entity Framework Code-First class. Both WinForms and ASP.NET Image Property Editors are used automatically for properties of the MediaDataObjecttype, no attributes are required (however, you still can apply the ImageEditorAttribute to customize the editor's options, as it is demonstated above for byte arrays). The use of this type reduces traffic because images are cached in the browser cache (compared to images of the byte[] type). The delayed loading is always used for MediaDataObjectproperties.

using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
// ...
[DefaultClassOptions]
public class Contact {
    [Browsable(false)]
    public int ID { get; private set; }
    public string Name { get; set; }
    public virtual MediaDataObject Photo { get; set; }
}
See Also