EntityKey Construtores
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Inicializa uma nova instância da classe EntityKey.
Sobrecargas
| Nome | Description |
|---|---|
| EntityKey() |
Inicializa uma nova instância da classe EntityKey. |
| EntityKey(String, IEnumerable<KeyValuePair<String,Object>>) |
Inicializa uma nova instância da EntityKey classe com um nome de conjunto de entidades e uma coleção genérica KeyValuePair . |
| EntityKey(String, IEnumerable<EntityKeyMember>) |
Inicializa uma nova instância da EntityKey classe com um nome de conjunto de entidades e uma IEnumerable<T> coleção de EntityKeyMember objetos. |
| EntityKey(String, String, Object) |
Inicializa uma nova instância da EntityKey classe com um nome de conjunto de entidades e um par de chaves de entidade específico. |
EntityKey()
Inicializa uma nova instância da classe EntityKey.
public:
EntityKey();
public EntityKey();
Public Sub New ()
Aplica-se a
EntityKey(String, IEnumerable<KeyValuePair<String,Object>>)
Inicializa uma nova instância da EntityKey classe com um nome de conjunto de entidades e uma coleção genérica 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)))
Parâmetros
- qualifiedEntitySetName
- String
Um String que é o nome do conjunto de entidades qualificado pelo nome do contêiner da entidade.
- entityKeyValues
- IEnumerable<KeyValuePair<String,Object>>
Uma coleção genérica KeyValuePair .
Cada par chave/valor tem um nome de propriedade como a chave e o valor dessa propriedade como o valor. Deve haver um par para cada propriedade que faz parte do EntityKey. A ordem dos pares chave/valor não é importante, mas cada propriedade de chave deve ser incluída. Os nomes de propriedade são nomes simples que não são qualificados com um nome de tipo de entidade ou o nome do esquema.
Exemplos
Este exemplo mostra como criar e usar um 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);
}
}
Aplica-se a
EntityKey(String, IEnumerable<EntityKeyMember>)
Inicializa uma nova instância da EntityKey classe com um nome de conjunto de entidades e uma IEnumerable<T> coleção de EntityKeyMember objetos.
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))
Parâmetros
- qualifiedEntitySetName
- String
Um String que é o nome do conjunto de entidades qualificado pelo nome do contêiner da entidade.
- entityKeyValues
- IEnumerable<EntityKeyMember>
Uma IEnumerable<T> coleção de EntityKeyMember objetos com os quais inicializar a chave.
Aplica-se a
EntityKey(String, String, Object)
Inicializa uma nova instância da EntityKey classe com um nome de conjunto de entidades e um par de chaves de entidade específico.
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)
Parâmetros
- qualifiedEntitySetName
- String
Um String que é o nome do conjunto de entidades qualificado pelo nome do contêiner da entidade.
Exemplos
Este exemplo mostra como criar e usar um 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);
}
}