/// <inheritdoc/> public ColumnPropertyInfo[] ResolveKeyProperties(Type type) { IEntityMap entityMap; if (!FluentMapper.EntityMaps.TryGetValue(type, out entityMap)) { return(DefaultResolver.ResolveKeyProperties(type)); } var mapping = entityMap as IDommelEntityMap; if (mapping != null) { var allPropertyMaps = entityMap.PropertyMaps.OfType <DommelPropertyMap>(); var keyPropertyInfos = allPropertyMaps .Where(e => e.Key) .Select(x => new ColumnPropertyInfo(x.PropertyInfo, isKey: true)) .ToArray(); // Now make sure there aren't any missing key properties that weren't explicitly defined in the mapping. try { // Make sure to exclude any keys that were defined in the dommel entity map and not marked as keys. var defaultKeyPropertyInfos = DefaultResolver.ResolveKeyProperties(type).Where(x => allPropertyMaps.Count(y => y.PropertyInfo.Equals(x.Property)) == 0); keyPropertyInfos = keyPropertyInfos.Union(defaultKeyPropertyInfos).ToArray(); } catch { // There could be no default Ids found. This is okay as long as we found a custom one. if (keyPropertyInfos.Length == 0) { throw new InvalidOperationException($"Could not find the key properties for type '{type.FullName}'."); } } return(keyPropertyInfos); } // Fall back to the default mapping strategy. return(DefaultResolver.ResolveKeyProperties(type)); }
/// <summary> /// Resolves the single key property for the specified type. /// </summary> /// <param name="keyPropertyResolver">The <see cref="IKeyPropertyResolver"/>.</param> /// <param name="type">The type to resolve the key property for.</param> /// <param name="isIdentity">Indicates whether the key properties are identity properties.</param> /// <returns>A <see cref="PropertyInfo"/> instance of the key property of <paramref name="type"/>.</returns> public static PropertyInfo ResolveKeyProperty(this IKeyPropertyResolver keyPropertyResolver, Type type, out bool isIdentity) => keyPropertyResolver.ResolveKeyProperties(type, out isIdentity).FirstOrDefault();
/// <summary> /// Resolves the single key property for the specified type. /// </summary> /// <param name="keyPropertyResolver">The <see cref="IKeyPropertyResolver"/>.</param> /// <param name="type">The type to resolve the key property for.</param> /// <returns>A <see cref="PropertyInfo"/> instance of the key property of <paramref name="type"/>.</returns> public static PropertyInfo ResolveKeyProperty(this IKeyPropertyResolver keyPropertyResolver, Type type) => keyPropertyResolver.ResolveKeyProperties(type).FirstOrDefault();