/// <summary> /// Gets a list of keys to group by. /// </summary> /// <remarks> /// When converting an unnormalized set of data from a database view, /// a new entity is only created when the grouping keys have changed. /// </remarks> /// <returns></returns> private GroupingKeyCollection GetGroupingKeyColumns() { // Get primary keys for this parent entity GroupingKeyCollection groupingKeyColumns = new GroupingKeyCollection(); groupingKeyColumns.PrimaryKeys.AddRange(Columns.PrimaryKeys); // The following conditions should fail with an exception: // 1) Any parent entity (entity with children) must have at least one PK specified or an exception will be thrown // 2) All 1-M relationship entities must have at least one PK specified // * Only 1-1 entities with no children are allowed to have 0 PKs specified. if ((groupingKeyColumns.PrimaryKeys.Count == 0 && _children.Count > 0) || (groupingKeyColumns.PrimaryKeys.Count == 0 && IsChild && _relationship.RelationshipInfo.RelationType == RelationshipTypes.Many) || groupingKeyColumns.PrimaryKeys.Count == 0 && !IsChild) { throw new MissingPrimaryKeyException(string.Format("There are no primary key mappings defined for the following entity: '{0}'.", this.EntityType.Name)); } // Add parent's keys if (IsChild) { groupingKeyColumns.ParentPrimaryKeys.AddRange(Parent.GroupingKeyColumns); } return(groupingKeyColumns); }
/// <summary> /// Concatenates the values of the GroupingKeys property and compares them /// against the LastKeyGroup property. Returns true if the values are different, /// or false if the values are the same. /// The currently concatenated keys are saved in the LastKeyGroup property. /// </summary> /// <param name="reader"></param> /// <returns></returns> public bool IsNewGroup(DbDataReader reader, bool useAltName) { bool isNewGroup = false; // Get primary keys from parent entity and any one-to-one child entites GroupingKeyCollection groupingKeyColumns = this.GroupingKeyColumns; // Concatenate column values KeyGroupInfo keyGroupInfo = groupingKeyColumns.CreateGroupingKey(reader, useAltName); if (!keyGroupInfo.HasNullKey && !_entityReferences.ContainsKey(keyGroupInfo.GroupingKey)) { isNewGroup = true; } return(isNewGroup); }