17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: <strong>FILESTREAM</strong> with Entity Framework and LINQ to <strong>SQL</strong>{using (SqlFileStream fs = new SqlFileStream(info.FilePath, info.Context, FileAccess.Read,FileOptions.WriteThrough, (long)info.Size)){fs.CopyTo(ms);}}}// TODO: Do some things with the memory stream,// or convert to byte array if serialization is required}tran.Commit();}}}}finally{if (OpenedConn) e.Connection.Close();}Listing 4-9:ReadItem method.This method first creates an Entity Framework object context (NorthPoleEntitiesclass) and retrieves the desired item. In this case, we used a lambda expression to indicatewhich item we are looking for. Lambda expressions may look a little intimidating, but thebasics can be learned easily. Here, we are indicating that, in the collection <strong>of</strong> all Items,we are looking for a single item that matches the predicate (think WHERE-clause) ItemID== ItemID, where the first ItemID is the scalar property and the second ItemID is theparameter value <strong>of</strong> the ReadItem function. We use the .SingleOrDefault() method toensure that if there is no item with the specified ItemID, the object context will return anull reference instead <strong>of</strong> throwing an exception.156

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!