방법: 프록시를 사용하여 POCO 엔터티 만들기(Entity Framework)
이 항목의 예제에서는 프록시를 사용하여 POCO 엔터티를 만드는 방법을 보여 줍니다. 프록시를 사용하여 POCO 엔터티를 만들려면 POCO 클래스가 POCO 프록시를 만들기 위한 요구 사항(Entity Framework)에 설명된 요구 사항을 충족해야 합니다.
이 항목의 예제에서는 방법: POCO 엔터티 정의(Entity Framework)에 정의된 POCO 클래스와 방법: 사용자 지정 개체를 사용할 수 있도록 모델링 및 매핑 파일 사용자 지정(Entity Framework)에 정의된 AdventureWorks 기반 데이터 모델을 사용합니다.
예제
이 예제에서는 CreateObject 메서드를 사용하여 새 LineItem 개체를 만든 다음 새 LineItem을 기존 Order에 추가합니다.
' 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());
}
}
참고 항목
작업
방법: POCO 엔터티가 프록시인지 식별(Entity Framework)
개념
POCO 엔터티 사용(Entity Framework)
POCO 엔터티에서 변경 내용 추적(Entity Framework)
개체 사용자 지정(Entity Framework)