ObjectStateManager.GetObjectStateEntry Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Retourne une ObjectStateEntry entrée d’objet ou de relation spécifique.
Surcharges
| Nom | Description |
|---|---|
| GetObjectStateEntry(EntityKey) |
Retourne une ObjectStateEntry entrée d’objet ou de relation avec la clé spécifiée. |
| GetObjectStateEntry(Object) |
Retourne une ObjectStateEntry valeur pour l’objet spécifié. |
GetObjectStateEntry(EntityKey)
Retourne une ObjectStateEntry entrée d’objet ou de relation avec la clé spécifiée.
public:
System::Data::Objects::ObjectStateEntry ^ GetObjectStateEntry(System::Data::EntityKey ^ key);
public System.Data.Objects.ObjectStateEntry GetObjectStateEntry(System.Data.EntityKey key);
member this.GetObjectStateEntry : System.Data.EntityKey -> System.Data.Objects.ObjectStateEntry
Public Function GetObjectStateEntry (key As EntityKey) As ObjectStateEntry
Paramètres
Retours
Correspondant ObjectStateEntry à l’élément donné EntityKey.
Exceptions
Quand key est null.
Lorsque le paramètre spécifié key est introuvable dans le gestionnaire d’état.
Aucune entité avec l’entité spécifiée EntityKey n’existe dans le ObjectStateManager.
Exemples
Cet exemple obtient la ObjectStateEntry valeur donnée EntityKey à partir du ObjectStateManager. Ensuite, elle obtient la valeur actuelle de la SalesOrderHeader.PurchaseOrderNumber propriété, modifie la valeur de la propriété et énumère la collection de propriétés modifiées.
// Specify the order to update.
int orderId = 43680;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
var order = (from o in context.SalesOrderHeaders
where o.SalesOrderID == orderId
select o).First();
// Change the status of an existing order.
order.Status = 1;
// Delete the first item in the order.
context.DeleteObject(order.SalesOrderDetails.First());
// Create a new SalesOrderDetail object.
// You can use the static CreateObjectName method (the Entity Framework
// adds this method to the generated entity types) instead of the new operator:
// SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
// Guid.NewGuid(), DateTime.Today));
SalesOrderDetail detail = new SalesOrderDetail
{
SalesOrderID = 0,
SalesOrderDetailID = 0,
OrderQty = 2,
ProductID = 750,
SpecialOfferID = 1,
UnitPrice = (decimal)2171.2942,
UnitPriceDiscount = 0,
LineTotal = 0,
rowguid = Guid.NewGuid(),
ModifiedDate = DateTime.Now
};
order.SalesOrderDetails.Add(detail);
// Get the ObjectStateEntry for the order.
ObjectStateEntry stateEntry =
context.ObjectStateManager
.GetObjectStateEntry(order);
Console.WriteLine("State before SaveChanges() is called: {0}",
stateEntry.State.ToString());
// Save changes in the object context to the database.
int changes = context.SaveChanges();
Console.WriteLine("State after SaveChanges() is called: {0}",
stateEntry.State.ToString());
Console.WriteLine(changes.ToString() + " changes saved!");
Console.WriteLine("Updated item for order ID: "
+ order.SalesOrderID.ToString());
// Iterate through the collection of SalesOrderDetail items.
foreach (SalesOrderDetail item in order.SalesOrderDetails)
{
Console.WriteLine("Item ID: "
+ item.SalesOrderDetailID.ToString() + " Product: "
+ item.ProductID.ToString() + " Quantity: "
+ item.OrderQty.ToString());
}
}
catch (UpdateException ex)
{
Console.WriteLine(ex.ToString());
}
}
Remarques
Utilisez la TryGetObjectStateEntry(EntityKey, ObjectStateEntry) méthode pour renvoyer un ObjectStateEntry objet sans avoir à gérer le InvalidOperationException déclenché par la GetObjectStateEntry(EntityKey) méthode.
S’applique à
GetObjectStateEntry(Object)
Retourne une ObjectStateEntry valeur pour l’objet spécifié.
public:
System::Data::Objects::ObjectStateEntry ^ GetObjectStateEntry(System::Object ^ entity);
public System.Data.Objects.ObjectStateEntry GetObjectStateEntry(object entity);
member this.GetObjectStateEntry : obj -> System.Data.Objects.ObjectStateEntry
Public Function GetObjectStateEntry (entity As Object) As ObjectStateEntry
Paramètres
- entity
- Object
Object Auquel appartient le récupéréObjectStateEntry.
Retours
Correspondant ObjectStateEntry à l’élément donné Object.
Exceptions
Aucune entité pour le spécifié Object n’existe dans le ObjectStateManager.
Remarques
Utilisez la TryGetObjectStateEntry(Object, ObjectStateEntry) méthode pour renvoyer un ObjectStateEntry objet sans avoir à gérer le InvalidOperationException déclenché par la GetObjectStateEntry(Object) méthode.