/// <summary> /// Throws an exception if the specified key type collection does not match the ones defined for the entity. /// </summary> /// <param name="source">The configurable conventions.</param> /// <param name="keyTypes">The key type collection to check against.</param> public static void ThrowsIfInvalidPrimaryKeyDefinition <T>([NotNull] this IRepositoryConventions source, [NotNull] params Type[] keyTypes) where T : class { Guard.NotNull(source, nameof(source)); Guard.NotEmpty(keyTypes, nameof(keyTypes)); var definedKeyInfos = source.GetPrimaryKeyPropertyInfos <T>().ToList(); if (!definedKeyInfos.Any()) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityRequiresPrimaryKey, typeof(T).FullName)); } if (definedKeyInfos.Count > 1) { var hasNoKeyOrdering = definedKeyInfos.Any(x => { var columnOrdering = source.GetColumnOrder(x); if (!columnOrdering.HasValue) { return(true); } return(columnOrdering.Value <= 0); }); if (hasNoKeyOrdering) { throw new InvalidOperationException(string.Format( Resources.UnableToDetermineCompositePrimaryKeyOrdering, typeof(T).FullName)); } } var definedKeyTypes = definedKeyInfos .Select(x => x.PropertyType) .ToArray(); if (keyTypes.Length != definedKeyTypes.Length || definedKeyTypes.Where((t, i) => t != keyTypes[i]).Any()) { throw new InvalidOperationException(Resources.EntityPrimaryKeyTypesMismatch); } }