ObjectStateManager.TryGetObjectStateEntry 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
嘗試回傳 ObjectStateEntry 特定物件或關聯項目的物件。
多載
| 名稱 | Description |
|---|---|
| TryGetObjectStateEntry(EntityKey, ObjectStateEntry) |
嘗試取得與指定EntityKey物件或關係的對應 ObjectStateEntry 。 |
| TryGetObjectStateEntry(Object, ObjectStateEntry) |
嘗試取得 ObjectStateEntry 對應 Object的 。 |
TryGetObjectStateEntry(EntityKey, ObjectStateEntry)
嘗試取得與指定EntityKey物件或關係的對應 ObjectStateEntry 。
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
參數
- entry
- ObjectStateEntry
當此方法回傳時,包含 ObjectStateEntry 對給定 EntityKey 的 ,此參數未初始化。
傳回
一個布林值,若true存在對應ObjectStateEntry的給定EntityKey;否則,。 false
例外狀況
key 提供 null(Visual Basic 中為 Nothing)值。
範例
以下範例嘗試檢索給定 EntityKey的對應 ObjectStateEntry 。
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");
}
}
以下範例使用 TryGetObjectStateEntry(EntityKey, ObjectStateEntry) 了 返回 上 ObjectStateManager 的方法,根據其實體鍵數取得物件。
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();
}
}
備註
用TryGetObjectStateEntry(EntityKey, ObjectStateEntry)來退貨,ObjectStateEntry無需處理該方法的提升InvalidOperationExceptionGetObjectStateEntry(EntityKey)。
適用於
TryGetObjectStateEntry(Object, ObjectStateEntry)
嘗試取得 ObjectStateEntry 對應 Object的 。
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
參數
- entity
- Object
Object取回ObjectStateEntry者所屬的。
- entry
- ObjectStateEntry
當此方法回傳時,包含 ObjectStateEntry 給定 EntityKey 參數,該參數未初始化。
傳回
一個布林值,若存在對應ObjectStateEntry的物件,則為true;否則,。 false
備註
使用TryGetObjectStateEntry(Object, ObjectStateEntry)該方法來返回,ObjectStateEntry而無需處理該方法所GetObjectStateEntry(Object)提出的部分InvalidOperationException。