ObjectStateManager.TryGetObjectStateEntry ã¡ã½ãã
å®ç¾©
éè¦
ä¸é¨ã®æ å ±ã¯ããªãªã¼ã¹åã«å¤§ãã夿´ãããå¯è½æ§ããããã¬ãªãªã¼ã¹ããã製åã«é¢ãããã®ã§ãã Microsoft ã¯ãããã«è¨è¼ããã¦ããæ å ±ã«ã¤ãã¦ãæç¤ºã¾ãã¯é»ç¤ºãåãããä¸åä¿è¨¼ãã¾ããã
ç¹å®ã®ãªãã¸ã§ã¯ãã¾ãã¯ãªã¬ã¼ã·ã§ã³ã·ãã ã¨ã³ããªã® ObjectStateEntry ãªãã¸ã§ã¯ããè¿ãã¾ãã
ãªã¼ãã¼ãã¼ã
TryGetObjectStateEntry(EntityKey, ObjectStateEntry) |
æå®ããã ObjectStateEntry ã®ãªãã¸ã§ã¯ãã¾ãã¯ãªã¬ã¼ã·ã§ã³ã·ããã«å¯¾å¿ãã EntityKey ã®åå¾ã試ã¿ã¾ãã |
TryGetObjectStateEntry(Object, ObjectStateEntry) |
æå®ããã ObjectStateEntry ã«å¯¾å¿ãã Object ã®åå¾ã試ã¿ã¾ãã |
TryGetObjectStateEntry(EntityKey, ObjectStateEntry)
æå®ããã ObjectStateEntry ã®ãªãã¸ã§ã¯ãã¾ãã¯ãªã¬ã¼ã·ã§ã³ã·ããã«å¯¾å¿ãã EntityKey ã®åå¾ã試ã¿ã¾ãã
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
ã
ä¾å¤
null
(Visual Basic ã§ã¯ Nothing
) å¤ã¯ key
ã«æä¾ããã¾ãã
ä¾
次ã®ä¾ã§ã¯ãæå®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 ã¡ã½ããã§çºçãã InvalidOperationException ãå¦çããã«ãGetObjectStateEntry(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 ãæ ¼ç´ããã¾ãããã®ãã©ã¡ã¼ã¿ã¼ã¯åæåããã¦ããªãç¶æ ã§æ¸¡ããã¾ãã
æ»ãå¤
æå®ããããªãã¸ã§ã¯ãã«å¯¾å¿ãã true
ãåå¨ããå ´å㯠ObjectStateEntry ã®ãã¼ã«å¤ããã以å¤ã®å ´å㯠false
ã
注é
TryGetObjectStateEntry(Object, ObjectStateEntry) ã¡ã½ããã使ç¨ãã¦ãObjectStateEntry ã¡ã½ããã§çºçãã InvalidOperationException ãå¦çããã«ãGetObjectStateEntry(Object) ãè¿ãã¾ãã