/// <summary> /// Creates a key structural value for a QueryEntityValue /// </summary> /// <param name="queryEntityValue">Query EntityValue to create the key from</param> /// <returns>A Query Key Structural Value</returns> public static QueryKeyStructuralValue Key(this QueryEntityValue queryEntityValue) { ExceptionUtilities.CheckArgumentNotNull(queryEntityValue, "queryEntityValue"); ExceptionUtilities.Assert(!queryEntityValue.IsNull, "queryEntityValue cannot be null"); return(new QueryKeyStructuralValue(queryEntityValue)); }
/// <summary> /// Initializes a new instance of the QueryKeyStructuralValue class. /// </summary> /// <param name="entityInstance">Entity Instance</param> internal QueryKeyStructuralValue(QueryEntityValue entityInstance) : base(entityInstance.Type, false, null, entityInstance.Type.EvaluationStrategy) { ExceptionUtilities.CheckArgumentNotNull(entityInstance, "entityInstance"); this.queryEntityType = entityInstance.Type as QueryEntityType; this.keyProperties = this.queryEntityType.Properties.Where(p => p.IsPrimaryKey).ToList(); ExceptionUtilities.CheckObjectNotNull(this.queryEntityType, "queryEntityType"); ExceptionUtilities.Assert(this.keyProperties.Count > 0, "There are no key properties on QueryEntityType '{0}'", this.queryEntityType); foreach (QueryProperty keyProperty in this.keyProperties) { this.SetValue(keyProperty.Name, entityInstance.GetScalarValue(keyProperty.Name)); } }
/// <summary> /// Creates a key structural value for a QueryEntityValue /// </summary> /// <param name="queryEntityType">Query Entity Type that has key metadata to extract key information</param> /// <param name="entityInstance">Entity Instance object value that contains the key</param> /// <returns>A Query Key Structural Value</returns> public static QueryKeyStructuralValue GetEntityInstanceKey(this QueryEntityType queryEntityType, object entityInstance) { ExceptionUtilities.CheckArgumentNotNull(queryEntityType, "queryEntityType"); ExceptionUtilities.CheckArgumentNotNull(entityInstance, "entityInstance"); ExceptionUtilities.Assert(queryEntityType.ClrType.IsAssignableFrom(entityInstance.GetType()), "entityInstance of type '{0}' is not assignable to queryEntityType '{1}'", entityInstance.GetType(), queryEntityType.EntityType.FullName); QueryEntityValue keyValueContainer = new QueryEntityValue(queryEntityType, false, null, queryEntityType.EvaluationStrategy); foreach (QueryProperty keyProperty in queryEntityType.Properties.Where(m => m.IsPrimaryKey)) { PropertyInfo pi = entityInstance.GetType().GetProperty(keyProperty.Name); keyValueContainer.SetPrimitiveValue(keyProperty.Name, pi.GetValue(entityInstance, null)); } return(new QueryKeyStructuralValue(keyValueContainer)); }