コード例 #1
0
    private static IReadOnlyList <string>?FindCandidateDependentPropertiesThroughNavigation(
        IConventionForeignKeyBuilder relationshipBuilder,
        bool pointsToPrincipal)
    {
        var navigation = pointsToPrincipal
            ? relationshipBuilder.Metadata.DependentToPrincipal
            : relationshipBuilder.Metadata.PrincipalToDependent !;

        var navigationFkAttribute = navigation != null
            ? GetForeignKeyAttribute(navigation)
            : null;

        if (navigationFkAttribute == null)
        {
            return(null);
        }

        var properties = navigationFkAttribute.Name.Split(',').Select(p => p.Trim()).ToList();

        if (properties.Any(p => string.IsNullOrWhiteSpace(p) || p == navigation !.Name))
        {
            throw new InvalidOperationException(
                      CoreStrings.InvalidPropertyListOnNavigation(
                          navigation !.Name, navigation.DeclaringEntityType.DisplayName(), navigationFkAttribute.Name));
        }

        var navigationPropertyTargetType =
            navigation !.DeclaringEntityType.GetRuntimeProperties()[navigation.Name].PropertyType;

        var otherNavigations = navigation.DeclaringEntityType.GetRuntimeProperties().Values
                               .Where(p => p.PropertyType == navigationPropertyTargetType && p.GetSimpleMemberName() != navigation.Name)
                               .OrderBy(p => p.GetSimpleMemberName());

        foreach (var propertyInfo in otherNavigations)
        {
            var attribute = GetAttribute <ForeignKeyAttribute>(propertyInfo);
            if (attribute?.Name == navigationFkAttribute.Name)
            {
                throw new InvalidOperationException(
                          CoreStrings.MultipleNavigationsSameFk(
                              navigation.DeclaringEntityType.DisplayName(),
                              attribute.Name,
                              $"'{navigation.Name}', '{propertyInfo.Name}'"));
            }
        }

        return(properties);
    }