Procedura: creare un'entità POCO con proxy (Entity Framework)
Nell'esempio di questo argomento viene illustrato come creare entità POCO con proxy. Per creare entità POCO con proxy, la classe POCO deve soddisfare i requisiti descritti in Requisiti per la creazione di proxy POCO (Entity Framework).
Nell'esempio di questo argomento vengono utilizzati le classi POCO definite in Procedura: definire entità POCO (Entity Framework) e un modello di dati basato su AdventureWorks definito in Procedura: personalizzare i file di mapping e di modellazione per l'utilizzo con oggetti personalizzati (Entity Framework).
Esempio
In questo esempio viene creato un nuovo oggetto LineItem tramite il metodo CreateObject; il nuovo LineItem viene quindi aggiunto a un'entità Order esistente.
' Specify the order to update.
Dim orderId As Integer = 43680
Using context As New POCOAdventureWorksEntities()
Try
' Enable lazy loading.
context.ContextOptions.LazyLoadingEnabled = True
Dim order As Order = context.Orders.Where(Function(o) o.SalesOrderID = orderId).First()
' Create a new item and add it to the order.
' The Entity Framework is going to generate
' proxy object for the newItem object.
Dim newItem As LineItem = context.CreateObject(Of LineItem)()
newItem.SalesOrderDetailID = 0
' Assign the order to the new LineItem.
newItem.SalesOrderID = orderId
newItem.OrderQty = 1
newItem.ProductID = 750
newItem.UnitPriceDiscount = 0
newItem.UnitPrice = 2171.2942D
newItem.ModifiedDate = DateTime.Today
newItem.rowguid = Guid.NewGuid()
newItem.SpecialOfferID = 1
' Add the new item to the order.
' The order will be added to the context because
' we are working with POCO proxies.
order.LineItems.Add(newItem)
' The state of the newItem is Added.
Console.WriteLine(context.ObjectStateManager.GetObjectStateEntry(newItem).State.ToString())
' Change the status and ship date of an existing order.
order.ShipDate = DateTime.Today
' The sate of the order item is Modified.
Console.WriteLine(context.ObjectStateManager.GetObjectStateEntry(order).State.ToString())
' The newItem is set to Unchanged.
context.SaveChanges()
' Change the newly added item.
newItem.OrderQty = 2
' The changes are tracked as they occur and the state of the object is Modified.
Console.WriteLine(context.ObjectStateManager.GetObjectStateEntry(newItem).State.ToString())
' Delete the newly created object.
context.DeleteObject(newItem)
' Save changes in the object context to the database
' after first detecting changes again.
context.SaveChanges()
Catch ex As UpdateException
Console.WriteLine(ex.ToString())
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
// Specify the order to update.
int orderId = 43680;
using (POCOAdventureWorksEntities context =
new POCOAdventureWorksEntities())
{
try
{
// Enable lazy loading.
context.ContextOptions.LazyLoadingEnabled = true;
Order order = context.Orders.
Where(o => o.SalesOrderID == orderId).First();
// Create a new item and add it to the order.
// The Entity Framework is going to generate
// proxy object for the newItem object.
LineItem newItem = context.CreateObject<LineItem>();
newItem.SalesOrderDetailID = 0;
// Assign the order to the new LineItem.
newItem.SalesOrderID = orderId;
newItem.OrderQty = 1;
newItem.ProductID = 750;
newItem.UnitPriceDiscount = 0;
newItem.UnitPrice = 2171.2942M;
newItem.ModifiedDate = DateTime.Today;
newItem.rowguid = Guid.NewGuid();
newItem.SpecialOfferID = 1;
// Add the new item to the order.
// The order will be added to the context because
// we are working with POCO proxies.
order.LineItems.Add(newItem);
// The state of the newItem is Added.
Console.WriteLine(context.ObjectStateManager.GetObjectStateEntry(newItem).State);
// Change the status and ship date of an existing order.
order.ShipDate = DateTime.Today;
// The sate of the order item is Modified.
Console.WriteLine(context.ObjectStateManager.GetObjectStateEntry(order).State);
// The newItem is set to Unchanged.
context.SaveChanges();
// Change the newly added item.
newItem.OrderQty = 2;
// The changes are tracked as they occur and the state of the object is Modified.
Console.WriteLine(context.ObjectStateManager.GetObjectStateEntry(newItem).State);
// Delete the newly created object.
context.DeleteObject(newItem);
// Save changes in the object context to the database
// after first detecting changes again.
context.SaveChanges();
}
catch (UpdateException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
Vedere anche
Attività
Procedura: stabilire se un'entità POCO è un proxy (Entity Framework)
Concetti
Utilizzo di entità POCO (Entity Framework)
Rilevamento delle modifiche nelle entità POCO (Entity Framework)
Personalizzazione di oggetti (Entity Framework)