本文整理汇总了C#中IEdmEntityType.Key方法的典型用法代码示例。如果您正苦于以下问题:C# IEdmEntityType.Key方法的具体用法?C# IEdmEntityType.Key怎么用?C# IEdmEntityType.Key使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEdmEntityType
的用法示例。
在下文中一共展示了IEdmEntityType.Key方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetKeyProperties
/// <summary>
/// Get key value pair array for specifc odata entry using specifc entity type
/// </summary>
/// <param name="entry">The entry instance.</param>
/// <param name="serializationInfo">The serialization info of the entry for writing without model.</param>
/// <param name="actualEntityType">The edm entity type of the entry</param>
/// <returns>Key value pair array</returns>
internal static KeyValuePair<string, object>[] GetKeyProperties(
ODataEntry entry,
ODataFeedAndEntrySerializationInfo serializationInfo,
IEdmEntityType actualEntityType)
{
KeyValuePair<string, object>[] keyProperties = null;
string actualEntityTypeName = null;
if (serializationInfo != null)
{
if (String.IsNullOrEmpty(entry.TypeName))
{
throw new ODataException(OData.Core.Strings.ODataFeedAndEntryTypeContext_ODataEntryTypeNameMissing);
}
actualEntityTypeName = entry.TypeName;
keyProperties = ODataEntryMetadataContextWithoutModel.GetPropertiesBySerializationInfoPropertyKind(entry, ODataPropertyKind.Key, actualEntityTypeName);
}
else
{
actualEntityTypeName = actualEntityType.FullName();
IEnumerable<IEdmStructuralProperty> edmKeyProperties = actualEntityType.Key();
if (edmKeyProperties != null)
{
keyProperties = edmKeyProperties.Select(p => new KeyValuePair<string, object>(p.Name, GetPrimitivePropertyClrValue(entry, p.Name, actualEntityTypeName, /*isKeyProperty*/false))).ToArray();
}
}
ValidateEntityTypeHasKeyProperties(keyProperties, actualEntityTypeName);
return keyProperties;
}
示例2: ResolveKeys
/// <summary>
/// Resolve keys for certain entity set, this function would be called when key is specified as positional values. E.g. EntitySet('key')
/// </summary>
/// <param name="type">Type for current entityset.</param>
/// <param name="positionalValues">The list of positional values.</param>
/// <param name="convertFunc">The convert function to be used for value converting.</param>
/// <returns>The resolved key list.</returns>
public virtual IEnumerable<KeyValuePair<string, object>> ResolveKeys(IEdmEntityType type, IList<string> positionalValues, Func<IEdmTypeReference, string, object> convertFunc)
{
var keyProperties = type.Key().ToList();
var keyPairList = new List<KeyValuePair<string, object>>(positionalValues.Count);
for (int i = 0; i < keyProperties.Count; i++)
{
string valueText = positionalValues[i];
IEdmProperty keyProperty = keyProperties[i];
object convertedValue = convertFunc(keyProperty.Type, valueText);
if (convertedValue == null)
{
throw ExceptionUtil.CreateSyntaxError();
}
keyPairList.Add(new KeyValuePair<string, object>(keyProperty.Name, convertedValue));
}
return keyPairList;
}
示例3: GetPathKeyValues
private static IReadOnlyDictionary<string, object> GetPathKeyValues(
KeyValuePathSegment keySegment,
IEdmEntityType entityType)
{
var result = new Dictionary<string, object>();
var keys = entityType.Key();
// TODO GitHubIssue#42 : Improve key parsing logic
// this parsing implementation does not allow key values to contain commas
// Depending on the WebAPI to make KeyValuePathSegment.Values collection public
// (or have the parsing logic public).
string[] values = keySegment.Value.Split(EntityKeySeparator);
if (values.Length > 1)
{
foreach (string value in values)
{
// Split key name and key value
var keyValues = value.Split(EntityKeyNameValueSeparator);
if (keyValues.Length != 2)
{
throw new InvalidOperationException(Resources.IncorrectKeyFormat);
}
// Validate the key name
if (!keys.Select(k => k.Name).Contains(keyValues[0]))
{
throw new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
Resources.KeyNotValidForEntityType,
keyValues[0],
entityType.Name));
}
result.Add(keyValues[0], ODataUriUtils.ConvertFromUriLiteral(keyValues[1], ODataVersion.V4));
}
}
else
{
// We just have the single key value
// Validate it has exactly one key
if (keys.Count() > 1)
{
throw new InvalidOperationException(Resources.MultiKeyValuesExpected);
}
var keyName = keys.First().Name;
result.Add(keyName, ODataUriUtils.ConvertFromUriLiteral(keySegment.Value, ODataVersion.V4));
}
return result;
}
示例4: BindKeyPropertyValue
/// <summary>
/// Binds a key property value.
/// </summary>
/// <param name="namedValue">The named value to bind.</param>
/// <param name="collectionItemEntityType">The type of a single item in a collection to apply the key value to.</param>
/// <returns>The bound key property value node.</returns>
private KeyPropertyValue BindKeyPropertyValue(NamedValue namedValue, IEdmEntityType collectionItemEntityType)
{
// These are exception checks because the data comes directly from the potentially user specified tree.
ExceptionUtils.CheckArgumentNotNull(namedValue, "namedValue");
ExceptionUtils.CheckArgumentNotNull(namedValue.Value, "namedValue.Value");
Debug.Assert(collectionItemEntityType != null, "collectionItemType != null");
IEdmProperty keyProperty = null;
if (namedValue.Name == null)
{
foreach (IEdmProperty p in collectionItemEntityType.Key())
{
if (keyProperty == null)
{
keyProperty = p;
}
else
{
throw new ODataException(Strings.MetadataBinder_UnnamedKeyValueOnTypeWithMultipleKeyProperties(collectionItemEntityType.ODataFullName()));
}
}
}
else
{
keyProperty = collectionItemEntityType.Key().Where(k => string.CompareOrdinal(k.Name, namedValue.Name) == 0).SingleOrDefault();
if (keyProperty == null)
{
throw new ODataException(Strings.MetadataBinder_PropertyNotDeclaredOrNotKeyInKeyValue(namedValue.Name, collectionItemEntityType.ODataFullName()));
}
}
IEdmTypeReference keyPropertyType = keyProperty.Type;
SingleValueQueryNode value = (SingleValueQueryNode)this.Bind(namedValue.Value);
// TODO: Check that the value is of primitive type
value = ConvertToType(value, keyPropertyType);
Debug.Assert(keyProperty != null, "keyProperty != null");
return new KeyPropertyValue()
{
KeyProperty = keyProperty,
KeyValue = value
};
}
示例5: BuildKeySegment
private static KeySegment BuildKeySegment(IEdmEntityType entityType, IEdmEntitySetBase entitySet, object target)
{
if (entityType == null)
{
throw new ArgumentNullException("entityType");
}
if (entitySet == null)
{
throw new ArgumentNullException("entitySet");
}
if (target == null)
{
throw new ArgumentNullException("target");
}
KeySegment keySegment = new KeySegment(
entityType.Key().Select(
(key) =>
{
var keyValue = target.GetType().GetProperty(key.Name).GetValue(target, null);
return new KeyValuePair<string, object>(key.Name, keyValue);
}),
entityType,
entitySet);
return keySegment;
}
示例6: WriteEntityType
internal void WriteEntityType(IEdmEntityType entityType, Dictionary<IEdmStructuredType, List<IEdmOperation>> boundOperationsMap)
{
var entityTypeName = entityType.Name;
entityTypeName = Context.EnableNamingAlias ? Customization.CustomizeNaming(entityTypeName) : entityTypeName;
WriteSummaryCommentForStructuredType(entityTypeName + SingleSuffix);
WriteStructurdTypeDeclaration(entityType,
ClassInheritMarker + string.Format(DataServiceQuerySingleStructureTemplate, GetFixedName(entityTypeName)),
SingleSuffix);
var singleTypeName = (Context.EnableNamingAlias ?
Customization.CustomizeNaming(entityType.Name) : entityType.Name) + SingleSuffix;
WriteConstructorForSingleType(GetFixedName(singleTypeName), string.Format(DataServiceQuerySingleStructureTemplate, GetFixedName(entityTypeName)));
var current = entityType;
while (current != null)
{
WritePropertiesForSingleType(current.DeclaredProperties);
current = (IEdmEntityType)current.BaseType;
}
WriteClassEndForStructuredType();
WriteSummaryCommentForStructuredType(Context.EnableNamingAlias ? Customization.CustomizeNaming(entityType.Name) : entityType.Name);
if (entityType.Key().Any())
{
var keyProperties = entityType.Key().Select(k => k.Name);
WriteKeyPropertiesCommentAndAttribute(
Context.EnableNamingAlias ? keyProperties.Select(k => Customization.CustomizeNaming(k)) : keyProperties,
string.Join("\", \"", keyProperties));
}
else
{
WriteEntityTypeAttribute();
}
if (Context.UseDataServiceCollection)
{
List<IEdmNavigationSource> navigationSourceList;
if (Context.ElementTypeToNavigationSourceMap.TryGetValue(entityType, out navigationSourceList))
{
if (navigationSourceList.Count == 1)
{
WriteEntitySetAttribute(navigationSourceList[0].Name);
}
}
}
if (entityType.HasStream)
{
WriteEntityHasStreamAttribute();
}
WriteStructurdTypeDeclaration(entityType, BaseEntityType);
SetPropertyIdentifierMappingsIfNameConflicts(entityType.Name, entityType);
WriteTypeStaticCreateMethod(entityType.Name, entityType);
WritePropertiesForStructuredType(entityType.DeclaredProperties);
if (entityType.BaseType == null && Context.UseDataServiceCollection)
{
WriteINotifyPropertyChangedImplementation();
}
WriteBoundOperations(entityType, boundOperationsMap);
WriteClassEndForStructuredType();
}
示例7: BuildKeyString
/// <summary>
/// Creates the key segment to use in the ID/EDitLink fields of an entry.
/// </summary>
/// <param name="entityInstance">The instance to get the key values from</param>
/// <param name="entityType">The entity type of the instance</param>
/// <param name="targetVersion">The OData version this segment is targeting.</param>
/// <returns>A Key segment that contains a literal encoded string key-value pairs for property names and their values</returns>
private static string BuildKeyString(object entityInstance, IEdmEntityType entityType, ODataVersion targetVersion)
{
if (entityType.DeclaredKey != null && entityType.DeclaredKey.Count() == 1)
{
var keyMember = entityType.Key().Single();
PropertyInfo property = entityInstance.GetType().GetProperty(keyMember.Name);
return ODataUriUtils.ConvertToUriLiteral(property.GetValue(entityInstance, null), targetVersion, DataSourceManager.GetCurrentDataSource().Model);
}
else
{
var keyStringFragments = new List<string>();
foreach (var keyMember in entityType.Key())
{
PropertyInfo property = entityInstance.GetType().GetProperty(keyMember.Name);
keyStringFragments.Add(String.Format("{0}={1}", keyMember.Name, ODataUriUtils.ConvertToUriLiteral(property.GetValue(entityInstance, null), targetVersion, DataSourceManager.GetCurrentDataSource().Model)));
}
return String.Join(",", keyStringFragments);
}
}
示例8: GetKeyInfos
private static string GetKeyInfos(int keyCount, string keyName, IEdmEntityType entityType, string keyPrefix, out IEdmTypeReference keyType)
{
Contract.Assert(keyName != null);
Contract.Assert(entityType != null);
string newKeyName;
IEdmStructuralProperty keyProperty;
if (String.IsNullOrEmpty(keyName))
{
Contract.Assert(keyCount == 1);
keyProperty = entityType.Key().First();
newKeyName = keyPrefix;
}
else
{
bool alternateKey = false;
keyProperty = entityType.Key().FirstOrDefault(k => k.Name == keyName);
if (keyProperty == null)
{
// If it's alternate key.
keyProperty =
entityType.Properties().OfType<IEdmStructuralProperty>().FirstOrDefault(p => p.Name == keyName);
alternateKey = true;
}
Contract.Assert(keyProperty != null);
// if there's only one key, just use the given prefix name, for example: "key, relatedKey"
// otherwise, to append the key name after the given prefix name.
// so for multiple keys, the parameter name is "keyId1, keyId2..."
// for navigation property, the parameter name is "relatedKeyId1, relatedKeyId2 ..."
// for alternate key, to append the alternate key name after the given prefix name.
if (alternateKey || keyCount > 1)
{
newKeyName = keyPrefix + keyName;
}
else
{
newKeyName = keyPrefix;
}
}
keyType = keyProperty.Type;
return newKeyName;
}
示例9: TryBindToKeys
/// <summary>
/// Binds key values to a key lookup on a collection.
/// </summary>
/// <param name="collectionNode">Already bound collection node.</param>
/// <param name="namedValues">The named value tokens to bind.</param>
/// <param name="model">The model to be used.</param>
/// <param name="collectionItemEntityType">The type of a single item in a collection to apply the key value to.</param>
/// <param name="keys">Dictionary of aliases to structural property names for the key.</param>
/// <param name="keyLookupNode">The bound key lookup.</param>
/// <returns>Returns true if binding succeeded.</returns>
private bool TryBindToKeys(EntityCollectionNode collectionNode, IEnumerable<NamedValue> namedValues, IEdmModel model, IEdmEntityType collectionItemEntityType, IDictionary<string, IEdmProperty> keys, out QueryNode keyLookupNode)
{
List<KeyPropertyValue> keyPropertyValues = new List<KeyPropertyValue>();
HashSet<string> keyPropertyNames = new HashSet<string>(StringComparer.Ordinal);
foreach (NamedValue namedValue in namedValues)
{
KeyPropertyValue keyPropertyValue;
if (!this.TryBindKeyPropertyValue(namedValue, collectionItemEntityType, keys, out keyPropertyValue))
{
keyLookupNode = null;
return false;
}
Debug.Assert(keyPropertyValue != null, "keyPropertyValue != null");
Debug.Assert(keyPropertyValue.KeyProperty != null, "keyPropertyValue.KeyProperty != null");
if (!keyPropertyNames.Add(keyPropertyValue.KeyProperty.Name))
{
throw new ODataException(ODataErrorStrings.MetadataBinder_DuplicitKeyPropertyInKeyValues(keyPropertyValue.KeyProperty.Name));
}
keyPropertyValues.Add(keyPropertyValue);
}
if (keyPropertyValues.Count == 0)
{
// No key values specified, for example '/Customers()', do not include the key lookup at all
keyLookupNode = collectionNode;
return true;
}
else if (keyPropertyValues.Count != collectionItemEntityType.Key().Count())
{
keyLookupNode = null;
return false;
}
else
{
keyLookupNode = new KeyLookupNode(collectionNode, new ReadOnlyCollection<KeyPropertyValue>(keyPropertyValues));
return true;
}
}
示例10: TryBindToDeclaredKey
/// <summary>
/// Tries to bind key values to a key lookup on a collection.
/// </summary>
/// <param name="collectionNode">Already bound collection node.</param>
/// <param name="namedValues">The named value tokens to bind.</param>
/// <param name="model">The model to be used.</param>
/// <param name="collectionItemEntityType">The type of a single item in a collection to apply the key value to.</param>
/// <param name="keyLookupNode">The bound key lookup.</param>
/// <returns>Returns true if binding succeeded.</returns>
private bool TryBindToDeclaredKey(EntityCollectionNode collectionNode, IEnumerable<NamedValue> namedValues, IEdmModel model, IEdmEntityType collectionItemEntityType, out QueryNode keyLookupNode)
{
Dictionary<string, IEdmProperty> keys = new Dictionary<string, IEdmProperty>(StringComparer.Ordinal);
foreach (IEdmStructuralProperty property in collectionItemEntityType.Key())
{
keys[property.Name] = property;
}
return TryBindToKeys(collectionNode, namedValues, model, collectionItemEntityType, keys, out keyLookupNode);
}
示例11: GetPropertiesToIncludeInQuery
private static ISet<IEdmStructuralProperty> GetPropertiesToIncludeInQuery(
SelectExpandClause selectExpandClause, IEdmEntityType entityType, out ISet<IEdmStructuralProperty> autoSelectedProperties)
{
autoSelectedProperties = new HashSet<IEdmStructuralProperty>();
HashSet<IEdmStructuralProperty> propertiesToInclude = new HashSet<IEdmStructuralProperty>();
PartialSelection selection = selectExpandClause.Selection as PartialSelection;
if (selection != null && !selection.SelectedItems.OfType<WildcardSelectionItem>().Any())
{
// only select requested properties and keys.
foreach (PathSelectionItem pathSelectionItem in selection.SelectedItems.OfType<PathSelectionItem>())
{
SelectExpandNode.ValidatePathIsSupported(pathSelectionItem.SelectedPath);
PropertySegment structuralPropertySegment = pathSelectionItem.SelectedPath.LastSegment as PropertySegment;
if (structuralPropertySegment != null)
{
propertiesToInclude.Add(structuralPropertySegment.Property);
}
}
// add keys
foreach (IEdmStructuralProperty keyProperty in entityType.Key())
{
if (!propertiesToInclude.Contains(keyProperty))
{
autoSelectedProperties.Add(keyProperty);
}
}
}
return propertiesToInclude;
}
示例12: ConvertToTaupoEntityType
private EntityType ConvertToTaupoEntityType(IEdmEntityType edmEntityType)
{
var taupoEntityType = new EntityType(edmEntityType.Namespace, edmEntityType.Name)
{
IsAbstract = edmEntityType.IsAbstract,
IsOpen = edmEntityType.IsOpen,
};
if (edmEntityType.BaseType != null)
{
taupoEntityType.BaseType = new EntityTypeReference(edmEntityType.BaseEntityType().Namespace, edmEntityType.BaseEntityType().Name);
}
foreach (var edmProperty in edmEntityType.DeclaredStructuralProperties())
{
var taupoProperty = this.ConvertToTaupoProperty(edmProperty);
taupoProperty.IsPrimaryKey = edmEntityType.Key().Contains(edmProperty);
taupoEntityType.Add(taupoProperty);
}
this.ConvertAnnotationsIntoTaupo(edmEntityType, taupoEntityType);
return taupoEntityType;
}
示例13: CompareEntityType
private void CompareEntityType(IEdmEntityType expectedEntityType, IEdmEntityType actualEntityType)
{
this.SatisfiesEquals(expectedEntityType.FullName(), actualEntityType.FullName(), "EntityType name does not match.");
this.SatisfiesEquals(expectedEntityType.IsAbstract, actualEntityType.IsAbstract, "IsAbstract does not match for EntityType '{0}'.", expectedEntityType.FullName());
string expectedBaseTypeName = expectedEntityType.BaseType != null ? ((IEdmSchemaElement)expectedEntityType.BaseType).FullName() : null;
string actualBaseTypeName = actualEntityType.BaseType != null ? ((IEdmSchemaElement)actualEntityType.BaseType).FullName() : null;
this.SatisfiesEquals(expectedBaseTypeName, actualBaseTypeName, "BaseType does not match for EntityType '{0}'.", expectedEntityType.FullName());
this.CompareProperties(expectedEntityType.StructuralProperties().Cast<IEdmProperty>(), actualEntityType.StructuralProperties().Cast<IEdmProperty>());
this.CompareProperties(expectedEntityType.Key().OfType<IEdmProperty>(), actualEntityType.Key().OfType<IEdmProperty>());
this.CompareNavigationProperty(expectedEntityType.Properties().OfType<IEdmNavigationProperty>(), actualEntityType.Properties().OfType<IEdmNavigationProperty>());
this.CompareTermAnnotations(expectedEntityType, actualEntityType);
}