HOW TO:套用對中斷連結的物件所做的變更 (Entity Framework)
本主題提供的範例將示範如何將相同物件的中斷連結執行個體所做的更新套用到物件。 在遠端更新物件,並設定回伺服器來保存變更時,會使用這個程序。 如果物件只附加到伺服器上的物件內容,則當此物件已經在物件內容中時,更新會遺失或是作業會失敗。 發生這種情況是因為物件是在 Unchanged 狀態下所附加。 如需詳細資訊,請參閱附加及中斷連結物件。
本主題的範例是根據 Adventure Works Sales Model。 若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。 若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:手動定義模型和對應檔 (Entity Framework) 中的程序。
範例
在下列範例中,會將更新的 SalesOrderDetail 物件傳遞給 UpdateItemChanges 方法,連同原始物件。 如此可套用變更,而不需要查詢此物件是否存在,或是將它保存在記憶體中。
Private Shared Sub ApplyItemUpdates(ByVal originalItem As SalesOrderDetail, ByVal updatedItem As SalesOrderDetail)
Using context As New AdventureWorksEntities()
context.SalesOrderDetails.Attach(updatedItem)
' Check if the ID is 0, if it is the item is new.
' In this case we need to chage the state to Added.
If updatedItem.SalesOrderDetailID = 0 Then
' Because the ID is generated by the database we do not need to
' set updatedItem.SalesOrderDetailID.
context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added)
Else
' If the SalesOrderDetailID is not 0, then the item is not new
' and needs to be updated. Because we already added the
' updated object to the context we need to apply the original values.
' If we attached originalItem to the context
' we would need to apply the current values:
' context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
' Applying current or original values, changes the state
' of the attached object to Modified.
context.ApplyOriginalValues("SalesOrderDetails", originalItem)
End If
context.SaveChanges()
End Using
End Sub
private static void ApplyItemUpdates(SalesOrderDetail originalItem,
SalesOrderDetail updatedItem)
{
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
context.SalesOrderDetails.Attach(updatedItem);
// Check if the ID is 0, if it is the item is new.
// In this case we need to chage the state to Added.
if (updatedItem.SalesOrderDetailID == 0)
{
// Because the ID is generated by the database we do not need to
// set updatedItem.SalesOrderDetailID.
context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
}
else
{
// If the SalesOrderDetailID is not 0, then the item is not new
// and needs to be updated. Because we already added the
// updated object to the context we need to apply the original values.
// If we attached originalItem to the context
// we would need to apply the current values:
// context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
// Applying current or original values, changes the state
// of the attached object to Modified.
context.ApplyOriginalValues("SalesOrderDetails", originalItem);
}
context.SaveChanges();
}
}
在此範例中,會擷取原始的 SalesOrderDetail 物件,然後根據傳遞給 UpdateItemChanges 方法的更新 SalesOrderDetail 物件將變更套用到原始物件。
Private Shared Sub ApplyItemUpdates(ByVal updatedItem As SalesOrderDetail)
' Define an ObjectStateEntry and EntityKey for the current object.
Dim key As EntityKey
Dim originalItem As Object
Using context As New AdventureWorksEntities()
' Create the detached object's entity key.
key = context.CreateEntityKey("SalesOrderDetails", updatedItem)
' Get the original item based on the entity key from the context
' or from the database.
If context.TryGetObjectByKey(key, originalItem) Then
' Call the ApplyCurrentValues method to apply changes
' from the updated item to the original version.
context.ApplyCurrentValues(key.EntitySetName, updatedItem)
End If
context.SaveChanges()
End Using
End Sub
private static void ApplyItemUpdates(SalesOrderDetail updatedItem)
{
// Define an ObjectStateEntry and EntityKey for the current object.
EntityKey key = default(EntityKey);
object originalItem = null;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Create the detached object's entity key.
key = context.CreateEntityKey("SalesOrderDetails", updatedItem);
// Get the original item based on the entity key from the context
// or from the database.
if (context.TryGetObjectByKey(key, out originalItem))
{
// Call the ApplyCurrentValues method to apply changes
// from the updated item to the original version.
context.ApplyCurrentValues(key.EntitySetName, updatedItem);
}
context.SaveChanges();
}
}
另請參閱
概念
建置 N-Tier 應用程式
序列化物件 (Entity Framework)
附加及中斷連結物件