How do I save a binary object (i.e., a graphics picture) as a database field in EF? RRS feed

  • Question


    VB 2010, EF 4.0, .NET 4.0, SQL Express


    If I want one of the fields in one of my entities to be a binary object--i.e., a picture or sound file--how do I set that up? (In the EF designer and at run time.) And should I save such info as pictures and sound in the database files, or should I saved them seperately and have file-name links to them in the database instead?

    Robert Gustafson

    Friday, January 24, 2014 4:38 PM

All replies

  • Depending on your workflow just add a column or property of the appropriate type:

    -in Database-First add a varbinary(max) column to your database

    -in Model-First add a column of type binary with Max Length = Max

    -in Code-First add a property of type byte[]

    And yes putting them in the database is fine. If you have a large number of large files SQL Server has some additional storage strategies you might investigate, like using a second filegroup for the blobs, or using FileStream for SQL to store the files in NTFS. 



    • Proposed as answer by JayChase Saturday, January 25, 2014 1:20 AM
    • Marked as answer by RobertGustafson Monday, January 27, 2014 12:13 PM
    • Unmarked as answer by RobertGustafson Wednesday, February 19, 2014 2:42 AM
    Friday, January 24, 2014 4:55 PM
  • Can you give me a VB 2010 code example of how one would, say, assign the image in a (Windows Forms) picture box to an EF entity field (or vice versa)?

    Robert Gustafson

    Wednesday, February 19, 2014 2:42 AM