EntityKey Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Poskytuje trvalý odkaz na objekt, který je instancí typu entity.
public ref class EntityKey sealed : IEquatable<System::Data::EntityKey ^>
[System.Runtime.Serialization.DataContract(IsReference=true)]
[System.Serializable]
public sealed class EntityKey : IEquatable<System.Data.EntityKey>
[<System.Runtime.Serialization.DataContract(IsReference=true)>]
[<System.Serializable>]
type EntityKey = class
interface IEquatable<EntityKey>
Public NotInheritable Class EntityKey
Implements IEquatable(Of EntityKey)
- Dědičnost
-
EntityKey
- Atributy
- Implementuje
Příklady
Tyto příklady ukazují, jak vytvořit a použít .EntityKey
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
Object entity = null;
IEnumerable<KeyValuePair<string, object>> entityKeyValues =
new KeyValuePair<string, object>[] {
new KeyValuePair<string, object>("SalesOrderID", 43680) };
// Create the key for a specific SalesOrderHeader object.
EntityKey key = new EntityKey("AdventureWorksEntities.SalesOrderHeaders", entityKeyValues);
// Get the object from the context or the persisted store by its key.
if (context.TryGetObjectByKey(key, out entity))
{
Console.WriteLine("The requested " + entity.GetType().FullName +
" object was found");
}
else
{
Console.WriteLine("An object with this key " +
"could not be found.");
}
}
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);
}
}
Poznámky
Objekty jsou neměnné. To EntityKey znamená, že po jejich vytvoření nelze upravit.
Konstruktory
| Name | Description |
|---|---|
| EntityKey() |
Inicializuje novou instanci EntityKey třídy. |
| EntityKey(String, IEnumerable<EntityKeyMember>) |
Inicializuje novou instanci EntityKey třídy s názvem sady entit a IEnumerable<T> kolekcí EntityKeyMember objektů. |
| EntityKey(String, IEnumerable<KeyValuePair<String,Object>>) |
Inicializuje novou instanci EntityKey třídy s názvem sady entit a obecnou KeyValuePair kolekcí. |
| EntityKey(String, String, Object) |
Inicializuje novou instanci EntityKey třídy s názvem sady entit a konkrétním párem klíčů entity. |
Pole
| Name | Description |
|---|---|
| EntityNotValidKey |
Jednoduchá EntityKey identifikace entity, která byla výsledkem neúspěšné operace TREAT . |
| NoEntitySetKey |
Jednoúčelový typ EntityKey , podle kterého je identifikována entita určená jen pro čtení. |
Vlastnosti
| Name | Description |
|---|---|
| EntityContainerName |
Získá nebo nastaví název kontejneru entity. |
| EntityKeyValues |
Získá nebo nastaví hodnoty klíče přidružené k tomuto EntityKey. |
| EntitySetName |
Získá nebo nastaví název sady entit. |
| IsTemporary |
Získá hodnotu, která označuje, zda EntityKey je dočasný. |
Metody
| Name | Description |
|---|---|
| Equals(EntityKey) |
Vrátí hodnotu, která určuje, zda je tato instance rovna zadanému EntityKey. |
| Equals(Object) |
Vrátí hodnotu, která určuje, zda je tato instance rovna zadanému objektu. |
| GetEntitySet(MetadataWorkspace) |
Získá entitu nastavenou pro tento klíč entity z daného pracovního prostoru metadat. |
| GetHashCode() |
Slouží jako hashovací funkce pro aktuální EntityKey objekt. GetHashCode() je vhodný pro algoritmy hash a datové struktury, jako je například hashovací tabulka. |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| OnDeserialized(StreamingContext) |
Pomocná metoda, která se používá k deserializaci EntityKey. |
| OnDeserializing(StreamingContext) |
Pomocná metoda, která se používá k deserializaci EntityKey. |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Operátory
| Name | Description |
|---|---|
| Equality(EntityKey, EntityKey) |
Porovná dva EntityKey objekty. |
| Inequality(EntityKey, EntityKey) |
Porovná dva EntityKey objekty. |