EntityKey Constructeurs
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.
Initialise une nouvelle instance de la classe EntityKey.
Surcharges
| Nom | Description |
|---|---|
| EntityKey() |
Initialise une nouvelle instance de la classe EntityKey. |
| EntityKey(String, IEnumerable<KeyValuePair<String,Object>>) |
Initialise une nouvelle instance de la EntityKey classe avec un nom d’ensemble d’entités et une collection générique KeyValuePair . |
| EntityKey(String, IEnumerable<EntityKeyMember>) |
Initialise une nouvelle instance de la EntityKey classe avec un nom d’ensemble d’entités et une IEnumerable<T> collection d’objets EntityKeyMember . |
| EntityKey(String, String, Object) |
Initialise une nouvelle instance de la EntityKey classe avec un nom d’ensemble d’entités et une paire de clés d’entité spécifique. |
EntityKey()
Initialise une nouvelle instance de la classe EntityKey.
public:
EntityKey();
public EntityKey();
Public Sub New ()
S’applique à
EntityKey(String, IEnumerable<KeyValuePair<String,Object>>)
Initialise une nouvelle instance de la EntityKey classe avec un nom d’ensemble d’entités et une collection générique KeyValuePair .
public:
EntityKey(System::String ^ qualifiedEntitySetName, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<System::String ^, System::Object ^>> ^ entityKeyValues);
public EntityKey(string qualifiedEntitySetName, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>> entityKeyValues);
new System.Data.EntityKey : string * seq<System.Collections.Generic.KeyValuePair<string, obj>> -> System.Data.EntityKey
Public Sub New (qualifiedEntitySetName As String, entityKeyValues As IEnumerable(Of KeyValuePair(Of String, Object)))
Paramètres
- qualifiedEntitySetName
- String
Nom String du jeu d’entités qualifié par le nom du conteneur d’entités.
- entityKeyValues
- IEnumerable<KeyValuePair<String,Object>>
Collection générique KeyValuePair .
Chaque paire clé/valeur a un nom de propriété comme clé et la valeur de cette propriété comme valeur. Il doit y avoir une paire pour chaque propriété qui fait partie du EntityKey. L’ordre des paires clé/valeur n’est pas important, mais chaque propriété de clé doit être incluse. Les noms de propriétés sont des noms simples qui ne sont pas qualifiés avec un nom de type d’entité ou le nom du schéma.
Exemples
Cet exemple montre comment créer et utiliser un EntityKey.
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Create the key that represents the order.
EntityKey orderKey =
new EntityKey("AdventureWorksEntities.SalesOrderHeaders",
"SalesOrderID", orderId);
// Create the stand-in SalesOrderHeader object
// based on the specified SalesOrderID.
SalesOrderHeader order = new SalesOrderHeader();
order.EntityKey = orderKey;
// Assign the ID to the SalesOrderID property to matche the key.
order.SalesOrderID = (int)orderKey.EntityKeyValues[0].Value;
// Attach the stand-in SalesOrderHeader object.
context.SalesOrderHeaders.Attach(order);
// 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 = orderId,
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);
context.SaveChanges();
}
catch (InvalidOperationException)
{
Console.WriteLine("Ensure that the key value matches the value of the object's ID property.");
}
catch (UpdateException)
{
Console.WriteLine("An error has occurred. Ensure that an object with the '{0}' key value exists.",
orderId);
}
}
S’applique à
EntityKey(String, IEnumerable<EntityKeyMember>)
Initialise une nouvelle instance de la EntityKey classe avec un nom d’ensemble d’entités et une IEnumerable<T> collection d’objets EntityKeyMember .
public:
EntityKey(System::String ^ qualifiedEntitySetName, System::Collections::Generic::IEnumerable<System::Data::EntityKeyMember ^> ^ entityKeyValues);
public EntityKey(string qualifiedEntitySetName, System.Collections.Generic.IEnumerable<System.Data.EntityKeyMember> entityKeyValues);
new System.Data.EntityKey : string * seq<System.Data.EntityKeyMember> -> System.Data.EntityKey
Public Sub New (qualifiedEntitySetName As String, entityKeyValues As IEnumerable(Of EntityKeyMember))
Paramètres
- qualifiedEntitySetName
- String
Nom String du jeu d’entités qualifié par le nom du conteneur d’entités.
- entityKeyValues
- IEnumerable<EntityKeyMember>
Collection IEnumerable<T> d’objets EntityKeyMember avec lesquels initialiser la clé.
S’applique à
EntityKey(String, String, Object)
Initialise une nouvelle instance de la EntityKey classe avec un nom d’ensemble d’entités et une paire de clés d’entité spécifique.
public:
EntityKey(System::String ^ qualifiedEntitySetName, System::String ^ keyName, System::Object ^ keyValue);
public EntityKey(string qualifiedEntitySetName, string keyName, object keyValue);
new System.Data.EntityKey : string * string * obj -> System.Data.EntityKey
Public Sub New (qualifiedEntitySetName As String, keyName As String, keyValue As Object)
Paramètres
- qualifiedEntitySetName
- String
Nom String du jeu d’entités qualifié par le nom du conteneur d’entités.
Exemples
Cet exemple montre comment créer et utiliser un EntityKey.
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Create the key that represents the order.
EntityKey orderKey =
new EntityKey("AdventureWorksEntities.SalesOrderHeaders",
"SalesOrderID", orderId);
// Create the stand-in SalesOrderHeader object
// based on the specified SalesOrderID.
SalesOrderHeader order = new SalesOrderHeader();
order.EntityKey = orderKey;
// Assign the ID to the SalesOrderID property to matche the key.
order.SalesOrderID = (int)orderKey.EntityKeyValues[0].Value;
// Attach the stand-in SalesOrderHeader object.
context.SalesOrderHeaders.Attach(order);
// 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 = orderId,
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);
context.SaveChanges();
}
catch (InvalidOperationException)
{
Console.WriteLine("Ensure that the key value matches the value of the object's ID property.");
}
catch (UpdateException)
{
Console.WriteLine("An error has occurred. Ensure that an object with the '{0}' key value exists.",
orderId);
}
}