如何:向/自二进制流对对象进行序列化和反序列化(实体框架)
由 实体数据模型 (EDM) 生成器工具 (EdmGen.exe) 和 Entity Data Model 向导生成的实体类型支持二进制序列化。 当将对象序列化为二进制流时,也将对当前加载到对象上下文中的所有相关对象进行序列化。 有关更多信息,请参见序列化对象(实体框架)。
本主题中的示例基于 Adventure Works 销售模型。 若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用实体框架。 为此,请完成如何:手动配置实体框架项目和如何:手动定义模型和映射文件(实体框架) 中的过程。 必须针对以下命名控件添加 using 语句(在 Visual Basic 中为 Imports):
示例
在本例中,SerializeToBinaryStream 方法查询 Contact 对象以获得指定的姓氏值,并返回二进制 MemoryStream。 MemoryStream 包含 Contact 对象及其相关的 SalesOrderHeader 和 SalesOrderDetail 对象的对象图。
Public Shared Sub ReadFromBinaryStream()
Dim formatter As New BinaryFormatter()
Using context As New AdventureWorksEntities()
Try
' Get the object graph for the selected customer
' as a binary stream.
Dim stream As MemoryStream = SerializeToBinaryStream("Adams")
' Read from the begining of the stream.
stream.Seek(0, SeekOrigin.Begin)
' Deserialize the customer graph from the binary stream
' and attach to an ObjectContext.
Dim contact As Contact = DirectCast(formatter.Deserialize(stream), Contact)
context.Attach(contact)
' Display information for each item
' in the orders that belong to the first contact.
For Each order As SalesOrderHeader In contact.SalesOrderHeaders
Console.WriteLine(String.Format("PO Number: {0}", order.PurchaseOrderNumber))
Console.WriteLine(String.Format("Order Date: {0}", order.OrderDate.ToString()))
Console.WriteLine("Order items:")
For Each item As SalesOrderDetail In order.SalesOrderDetails
Console.WriteLine(String.Format("Product: {0}, Quantity: {1}", _
item.ProductID.ToString(), item.OrderQty.ToString()))
Next
Next
Catch ex As SerializationException
Console.WriteLine("The object graph could not be deserialized from " & _
"the binary stream because of the following error:")
Console.WriteLine(ex.ToString())
End Try
End Using
End Sub
Private Shared Function SerializeToBinaryStream(ByVal lastName As String) As MemoryStream
Dim formatter As New BinaryFormatter()
Dim stream As New MemoryStream()
Using context As New AdventureWorksEntities()
' Specify a timeout for queries in this context, in seconds.
context.CommandTimeout = 120
' Define a customer contact.
Dim customer As Contact
' Create a Contact query with a path that returns
' orders and items for a contact.
Dim query As ObjectQuery(Of Contact) = context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails")
Try
' Return the first contact with the specified last name
' along with its related orders and items.
customer = query.Where("it.LastName = @lastname", New ObjectParameter("lastname", lastName)).First()
' Serialize the customer object graph.
formatter.Serialize(stream, customer)
Catch ex As EntitySqlException
Throw New InvalidOperationException("The object query failed", ex)
Catch ex As EntityCommandExecutionException
Throw New InvalidOperationException("The object query failed", ex)
Catch ex As SerializationException
Throw New InvalidOperationException("The object graph could not be serialized", ex)
End Try
' Return the streamed object graph.
Return stream
End Using
End Function
public static void ReadFromBinaryStream()
{
BinaryFormatter formatter = new BinaryFormatter();
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
try
{
// Get the object graph for the selected customer
// as a binary stream.
MemoryStream stream = SerializeToBinaryStream(@"Adams");
// Read from the begining of the stream.
stream.Seek(0, SeekOrigin.Begin);
// Deserialize the customer graph from the binary stream
// and attach to an ObjectContext.
Contact contact = (Contact)formatter.Deserialize(stream);
context.Attach(contact);
// Display information for each item
// in the orders that belong to the first contact.
foreach (SalesOrderHeader order in contact.SalesOrderHeaders)
{
Console.WriteLine(String.Format("PO Number: {0}",
order.PurchaseOrderNumber));
Console.WriteLine(String.Format("Order Date: {0}",
order.OrderDate.ToString()));
Console.WriteLine("Order items:");
foreach (SalesOrderDetail item in order.SalesOrderDetails)
{
Console.WriteLine(String.Format("Product: {0} "
+ "Quantity: {1}", item.ProductID.ToString(),
item.OrderQty.ToString()));
}
}
}
catch (SerializationException ex)
{
Console.WriteLine("The object graph could not be deserialized from "
+ "the binary stream because of the following error:");
Console.WriteLine(ex.ToString());
}
}
}
private static MemoryStream SerializeToBinaryStream(string lastName)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Specify a timeout for queries in this context, in seconds.
context.CommandTimeout = 120;
// Define a customer contact.
Contact customer;
// Create a Contact query with a path that returns
// orders and items for a contact.
ObjectQuery<Contact> query =
context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails");
try
{
// Return the first contact with the specified last name
// along with its related orders and items.
customer = query.Where("it.LastName = @lastname",
new ObjectParameter("lastname", lastName)).First();
// Serialize the customer object graph.
formatter.Serialize(stream, customer);
}
catch (EntitySqlException ex)
{
throw new InvalidOperationException("The object query failed", ex);
}
catch (EntityCommandExecutionException ex)
{
throw new InvalidOperationException("The object query failed", ex);
}
catch (SerializationException ex)
{
throw new InvalidOperationException("The object graph could not be serialized", ex);
}
// Return the streamed object graph.
return stream;
}
}