ObjectStateManager.TryGetObjectStateEntry Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Próbuje zwrócić ObjectStateEntry obiekt dla określonego obiektu lub wpisu relacji.
Przeciążenia
| TryGetObjectStateEntry(EntityKey, ObjectStateEntry) |
Próbuje pobrać odpowiedni ObjectStateEntry obiekt lub relację z określonym EntityKeyelementem . |
| TryGetObjectStateEntry(Object, ObjectStateEntry) |
Próbuje pobrać odpowiedni ObjectStateEntry element dla określonego Objectelementu . |
TryGetObjectStateEntry(EntityKey, ObjectStateEntry)
Próbuje pobrać odpowiedni ObjectStateEntry obiekt lub relację z określonym EntityKeyelementem .
public:
bool TryGetObjectStateEntry(System::Data::EntityKey ^ key, [Runtime::InteropServices::Out] System::Data::Objects::ObjectStateEntry ^ % entry);
public bool TryGetObjectStateEntry (System.Data.EntityKey key, out System.Data.Objects.ObjectStateEntry entry);
member this.TryGetObjectStateEntry : System.Data.EntityKey * ObjectStateEntry -> bool
Public Function TryGetObjectStateEntry (key As EntityKey, ByRef entry As ObjectStateEntry) As Boolean
Parametry
- entry
- ObjectStateEntry
Gdy ta metoda zwraca wartość , zawiera element ObjectStateEntry dla danego EntityKey parametru Ten parametr jest przekazywany niezainicjowany.
Zwraca
Wartość logiczna, która ma true wartość, jeśli jest odpowiednia ObjectStateEntry dla danej EntityKeywartości ; falsew przeciwnym razie .
Wyjątki
Wartość A null (Nothing w Visual Basic) jest udostępniana dla keyelementu .
Przykłady
Przykłady w tym temacie są oparte na Microsoft SQL Server Przykłady produktów: Baza danych.
W przykładzie podjęto próbę pobrania odpowiedniego ObjectStateEntry elementu dla danego EntityKeyelementu .
int orderId = 43680;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
ObjectStateManager objectStateManager = context.ObjectStateManager;
ObjectStateEntry stateEntry = null;
var order = (from o in context.SalesOrderHeaders
where o.SalesOrderID == orderId
select o).First();
// Attempts to retrieve ObjectStateEntry for the given EntityKey.
bool isPresent = objectStateManager.TryGetObjectStateEntry(((IEntityWithKey)order).EntityKey, out stateEntry);
if (isPresent)
{
Console.WriteLine("The entity was found");
}
}
Dim orderId As Integer = 43680
Using context As New AdventureWorksEntities()
Dim objectStateManager As ObjectStateManager = context.ObjectStateManager
Dim stateEntry As ObjectStateEntry = Nothing
Dim order = (From o In context.SalesOrderHeaders
Where o.SalesOrderID = orderId
Select o).First()
' Attempts to retrieve ObjectStateEntry for the given EntityKey.
Dim isPresent As Boolean = objectStateManager.TryGetObjectStateEntry(DirectCast(order, IEntityWithKey).EntityKey, stateEntry)
If isPresent Then
Console.WriteLine("The entity was found")
End If
End Using
W poniższym przykładzie użyto TryGetObjectStateEntry(EntityKey, ObjectStateEntry) ObjectStateManager metody zwróconej w celu pobrania obiektu na podstawie klucza jednostki.
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();
}
}
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
Uwagi
Służy TryGetObjectStateEntry(EntityKey, ObjectStateEntry) do zwracania elementu ObjectStateEntry bez konieczności obsługi elementu zgłoszonego InvalidOperationException przez metodę GetObjectStateEntry(EntityKey) .
Dotyczy
TryGetObjectStateEntry(Object, ObjectStateEntry)
Próbuje pobrać odpowiedni ObjectStateEntry element dla określonego Objectelementu .
public:
bool TryGetObjectStateEntry(System::Object ^ entity, [Runtime::InteropServices::Out] System::Data::Objects::ObjectStateEntry ^ % entry);
public bool TryGetObjectStateEntry (object entity, out System.Data.Objects.ObjectStateEntry entry);
member this.TryGetObjectStateEntry : obj * ObjectStateEntry -> bool
Public Function TryGetObjectStateEntry (entity As Object, ByRef entry As ObjectStateEntry) As Boolean
Parametry
- entity
- Object
Element Object , do którego należy pobrany ObjectStateEntry element.
- entry
- ObjectStateEntry
Gdy ta metoda zwraca wartość , element zawiera ObjectStateEntry wartość dla danego EntityKey parametru Ten parametr jest przekazywany niezainicjowany.
Zwraca
Wartość logiczna, która ma true wartość, jeśli istnieje odpowiednia ObjectStateEntry dla danego obiektu; falsew przeciwnym razie .
Uwagi
TryGetObjectStateEntry(Object, ObjectStateEntry) Użyj metody , aby zwrócić obiekt ObjectStateEntry bez konieczności obsługi elementu zgłoszonego InvalidOperationException przez metodę GetObjectStateEntry(Object) .