Exemplo n.º 1
0
        /// <summary>
        /// Finds the binding for the given navigation property.
        /// </summary>
        /// <param name="autoCreate">Tells whether the binding should be auto created if it does not exist.</param>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                                          bool autoCreate)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            NavigationPropertyBindingConfiguration bindingConfiguration;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out bindingConfiguration))
            {
                return(bindingConfiguration);
            }

            if (!autoCreate)
            {
                return(null);
            }

            bool hasSingletonAttribute = navigationConfiguration.PropertyInfo.GetCustomAttributes <SingletonAttribute>().Any();
            Type entityType            = navigationConfiguration.RelatedClrType;

            INavigationSourceConfiguration[] matchedNavigationSources;
            if (hasSingletonAttribute)
            {
                matchedNavigationSources = _modelBuilder.Singletons.Where(es => es.EntityType.ClrType == entityType).ToArray();
            }
            else
            {
                matchedNavigationSources = _modelBuilder.EntitySets.Where(es => es.EntityType.ClrType == entityType).ToArray();
            }

            if (matchedNavigationSources.Length == 1)
            {
                return(AddBinding(navigationConfiguration, matchedNavigationSources[0]));
            }
            else if (matchedNavigationSources.Length == 0)
            {
                return(null);
            }
            else
            {
                throw Error.NotSupported(
                          SRResources.CannotAutoCreateMultipleCandidates,
                          navigationConfiguration.Name,
                          navigationConfiguration.DeclaringEntityType.FullName,
                          Name,
                          String.Join(", ", matchedNavigationSources.Select(s => s.Name)));
            }
        }
        private NavigationPropertyConfiguration AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity, bool containsTarget)
        {
            if (navigationProperty == null)
            {
                throw Error.ArgumentNull("navigationProperty");
            }

            if (!navigationProperty.DeclaringType.IsAssignableFrom(ClrType))
            {
                throw Error.Argument("navigationProperty", SRResources.PropertyDoesNotBelongToType, navigationProperty.Name, ClrType.FullName);
            }

            ValidatePropertyNotAlreadyDefinedInBaseTypes(navigationProperty);
            ValidatePropertyNotAlreadyDefinedInDerivedTypes(navigationProperty);

            PropertyConfiguration           propertyConfig;
            NavigationPropertyConfiguration navigationPropertyConfig;

            if (ExplicitProperties.ContainsKey(navigationProperty))
            {
                propertyConfig = ExplicitProperties[navigationProperty];
                if (propertyConfig.Kind != PropertyKind.Navigation)
                {
                    throw Error.Argument("navigationProperty", SRResources.MustBeNavigationProperty, navigationProperty.Name, ClrType.FullName);
                }

                navigationPropertyConfig = propertyConfig as NavigationPropertyConfiguration;
                if (navigationPropertyConfig.Multiplicity != multiplicity)
                {
                    throw Error.Argument("navigationProperty", SRResources.MustHaveMatchingMultiplicity, navigationProperty.Name, multiplicity);
                }
            }
            else
            {
                navigationPropertyConfig = new NavigationPropertyConfiguration(
                    navigationProperty,
                    multiplicity,
                    this);
                if (containsTarget)
                {
                    navigationPropertyConfig = navigationPropertyConfig.Contained();
                }

                ExplicitProperties[navigationProperty] = navigationPropertyConfig;
                // make sure the related type is configured
                ModelBuilder.AddEntityType(navigationPropertyConfig.RelatedClrType);
            }
            return(navigationPropertyConfig);
        }
Exemplo n.º 3
0
        private void VerifyBindingPath(NavigationPropertyConfiguration navigationConfiguration, IList <object> bindingPath)
        {
            Contract.Assert(navigationConfiguration != null);
            Contract.Assert(bindingPath != null);

            PropertyInfo navigation = bindingPath.Last() as PropertyInfo;

            if (navigation == null || navigation != navigationConfiguration.PropertyInfo)
            {
                throw Error.Argument("navigationConfiguration", "TODO: " /*SRResources.NavigationPropertyBindingPathIsNotValid,
                                                                          * bindingPath.ConvertBindingPath(), navigationConfiguration.Name*/);
            }

            bindingPath.Aggregate(EntityType.ClrType, VerifyBindingSegment);
        }
Exemplo n.º 4
0
        public void HasNavigationPropertyLink(NavigationPropertyConfiguration navigationProperty,
                                              Func <ResourceContext <TEntityType>, IEdmNavigationProperty, Uri> navigationLinkFactory, bool followsConventions)
        {
            if (navigationProperty == null)
            {
                throw Error.ArgumentNull("navigationProperty");
            }

            if (navigationLinkFactory == null)
            {
                throw Error.ArgumentNull("navigationLinkFactory");
            }

            _configuration.HasNavigationPropertyLink(navigationProperty, new NavigationLinkBuilder((context, property) =>
                                                                                                   navigationLinkFactory(UpCastEntityContext(context), property), followsConventions));
        }
Exemplo n.º 5
0
        private void DoIt(NavigationPropertyConfiguration navProp, EdmEntityType type)
        {
            foreach (var dependentProperty in navProp.DependentProperties)
            {
                if (dependentProperty.GetConfiguration(_configurations).IsIgnored)
                {
                    navProp.IsIgnored = true;
                    return;
                }
            }
            if (navProp.IsIgnored)
            {
                return;
            }
            EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
            {
                Name = navProp.Name,
                TargetMultiplicity = navProp.Multiplicity,
                Target             = GetEdmType(navProp.RelatedClrType) as IEdmEntityType,
                ContainsTarget     = navProp.ContainsTarget,
                OnDelete           = navProp.OnDeleteAction
            };

            // Principal properties
            if (navProp.PrincipalProperties.Any())
            {
                info.PrincipalProperties = GetDeclaringPropertyInfo(navProp.PrincipalProperties);
            }

            // Dependent properties
            if (navProp.DependentProperties.Any())
            {
                info.DependentProperties = GetDeclaringPropertyInfo(navProp.DependentProperties);
            }

            IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);

            if (navProp.PropertyInfo != null && edmProperty != null)
            {
                _properties[navProp.PropertyInfo] = edmProperty;
            }

            if (edmProperty != null && navProp.IsRestricted)
            {
                _propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
            }
        }
Exemplo n.º 6
0
        public NavigationPropertyConfiguration HasRequired <TTargetEntity>(
            Expression <Func <TStructuralType, TTargetEntity> > navigationPropertyExpression,
            Expression <Func <TStructuralType, TTargetEntity, bool> > referentialConstraintExpression) where TTargetEntity : class
        {
            NavigationPropertyConfiguration navigation =
                GetOrCreateNavigationProperty(navigationPropertyExpression, EdmMultiplicity.One);

            IDictionary <PropertyInfo, PropertyInfo> referentialConstraints =
                PropertyPairSelectorVisitor.GetSelectedProperty(referentialConstraintExpression);

            foreach (KeyValuePair <PropertyInfo, PropertyInfo> constraint in referentialConstraints)
            {
                navigation.HasConstraint(constraint);
            }

            return(navigation);
        }
Exemplo n.º 7
0
        private static void FindNavigationProperties(this ODataModelBuilder builder, StructuralTypeConfiguration configuration,
                                                     IList <Tuple <StructuralTypeConfiguration, IList <object>, NavigationPropertyConfiguration> > navs,
                                                     Stack <object> path)
        {
            Contract.Assert(builder != null);
            Contract.Assert(configuration != null);
            Contract.Assert(navs != null);
            Contract.Assert(path != null);

            foreach (var property in configuration.Properties)
            {
                path.Push(property.PropertyInfo);

                NavigationPropertyConfiguration nav        = property as NavigationPropertyConfiguration;
                ComplexPropertyConfiguration    complex    = property as ComplexPropertyConfiguration;
                CollectionPropertyConfiguration collection = property as CollectionPropertyConfiguration;

                if (nav != null)
                {
                    // how about the containment?
                    IList <object> bindingPath = path.Reverse().ToList();

                    navs.Add(
                        new Tuple <StructuralTypeConfiguration, IList <object>, NavigationPropertyConfiguration>(configuration,
                                                                                                                 bindingPath, nav));
                }
                else if (complex != null)
                {
                    StructuralTypeConfiguration complexType = builder.GetTypeConfigurationOrNull(complex.RelatedClrType) as StructuralTypeConfiguration;
                    builder.FindAllNavigationProperties(complexType, navs, path);
                }
                else if (collection != null)
                {
                    IEdmTypeConfiguration edmType = builder.GetTypeConfigurationOrNull(collection.ElementType);
                    if (edmType != null && edmType.Kind == EdmTypeKind.Complex)
                    {
                        StructuralTypeConfiguration complexType = (StructuralTypeConfiguration)edmType;

                        builder.FindAllNavigationProperties(complexType, navs, path);
                    }
                }

                path.Pop();
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Removes the binding for the given navigation property and the given binding path.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="bindingPath">The binding path.</param>
        public virtual void RemoveBinding(NavigationPropertyConfiguration navigationConfiguration, string bindingPath)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            Dictionary <string, NavigationPropertyBindingConfiguration> navigationPropertyBindingMap;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBindingMap))
            {
                navigationPropertyBindingMap.Remove(bindingPath);

                if (!navigationPropertyBindingMap.Any())
                {
                    _navigationPropertyBindings.Remove(navigationConfiguration);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationPropertyBindingConfiguration"/> class.
        /// </summary>
        /// <param name="navigationProperty">The navigation property for the binding.</param>
        /// <param name="navigationSource">The target navigation source of the binding.</param>
        /// <param name="path">The path of current binding.</param>
        public NavigationPropertyBindingConfiguration(NavigationPropertyConfiguration navigationProperty,
                                                      NavigationSourceConfiguration navigationSource, IList <object> path)
        {
            if (navigationProperty == null)
            {
                throw new ArgumentNullException("navigationProperty");
            }

            if (navigationSource == null)
            {
                throw new ArgumentNullException("navigationSource");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            NavigationProperty     = navigationProperty;
            TargetNavigationSource = navigationSource;
            Path = path;
        }
        public NavigationPropertyBindingConfiguration HasOptionalBinding <TTargetType, TDerivedType>(
            Expression <Func <TDerivedType, TTargetType> > navigationExpression,
            string targetEntitySet)
            where TTargetType : class
            where TDerivedType : class, TStructuralType
        {
            if (navigationExpression == null)
            {
                throw Error.ArgumentNull("navigationExpression");
            }

            if (String.IsNullOrEmpty(targetEntitySet))
            {
                throw Error.ArgumentNullOrEmpty("targetEntitySet");
            }

            StructuralTypeConfiguration <TDerivedType> derivedConfiguration;

            if (this._structuralType.Configuration.Kind == EdmTypeKind.Entity)
            {
                derivedConfiguration = _modelBuilder.EntityType <TDerivedType>().DerivesFrom <TStructuralType>();
            }
            else
            {
                derivedConfiguration = _modelBuilder.ComplexType <TDerivedType>().DerivesFrom <TStructuralType>();
            }

            NavigationPropertyConfiguration navigation = derivedConfiguration.HasOptional(navigationExpression);

            IList <object> bindingPath = new List <object>(_bindingPath);

            bindingPath.Add(typeof(TDerivedType));
            bindingPath.Add(navigation.PropertyInfo);

            NavigationSourceConfiguration entitySet = _modelBuilder.EntitySet <TTargetType>(targetEntitySet).Configuration;

            return(this._navigationSource.AddBinding(navigation, entitySet, bindingPath));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Binds the given navigation property to the target navigation source.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetNavigationSource">The target navigation source.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                                         INavigationSourceConfiguration targetNavigationSource)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (targetNavigationSource == null)
            {
                throw Error.ArgumentNull("targetNavigationSource");
            }

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;

            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyNotInHierarchy,
                                     declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBindingConfiguration navigationPropertyBinding;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBinding))
            {
                if (navigationPropertyBinding.TargetNavigationSource != targetNavigationSource)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration, targetNavigationSource);
                _navigationPropertyBindings[navigationConfiguration] = navigationPropertyBinding;
            }

            return(navigationPropertyBinding);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Binds the given navigation property to the target navigation source.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetNavigationSource">The target navigation source.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                                         NavigationSourceConfiguration targetNavigationSource)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (targetNavigationSource == null)
            {
                throw Error.ArgumentNull("targetNavigationSource");
            }

            IList <object> bindingPath = new List <object> {
                navigationConfiguration.PropertyInfo
            };

            if (navigationConfiguration.DeclaringType != EntityType)
            {
                bindingPath.Insert(0, navigationConfiguration.DeclaringType.ClrType);
            }

            return(AddBinding(navigationConfiguration, targetNavigationSource, bindingPath));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Configures the navigation link for the given navigation property for this navigation source.
        /// </summary>
        /// <param name="navigationProperty">The navigation property for which the navigation link is being generated.</param>
        /// <param name="navigationLinkBuilder">The builder used to generate the navigation link.</param>
        /// <returns>Returns itself so that multiple calls can be chained.</returns>
        public virtual NavigationSourceConfiguration HasNavigationPropertyLink(NavigationPropertyConfiguration navigationProperty,
                                                                               NavigationLinkBuilder navigationLinkBuilder)
        {
            if (navigationProperty == null)
            {
                throw Error.ArgumentNull("navigationProperty");
            }

            if (navigationLinkBuilder == null)
            {
                throw Error.ArgumentNull("navigationLinkBuilder");
            }

            StructuralTypeConfiguration declaringType = navigationProperty.DeclaringType;

            if (!(declaringType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringType)))
            {
                throw Error.Argument("navigationProperty", SRResources.NavigationPropertyNotInHierarchy,
                                     declaringType.FullName, EntityType.FullName, Name);
            }

            _navigationPropertyLinkBuilders[navigationProperty] = navigationLinkBuilder;
            return(this);
        }
 /// <summary>
 /// Finds the binding for the given navigation property and tries to create it if it doesnot exist.
 /// </summary>
 /// <param name="navigationConfiguration">The navigation property.</param>
 /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
 public virtual NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration)
 {
     return FindBinding(navigationConfiguration, autoCreate: true);
 }
        /// <summary>
        /// Removes the binding for the given navigation property.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property</param>
        public virtual void RemoveBinding(NavigationPropertyConfiguration navigationConfiguration)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            _navigationPropertyBindings.Remove(navigationConfiguration);
        }
        /// <summary>
        /// Binds the given navigation property to the target navigation source.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="targetNavigationSource">The target navigation source.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration AddBinding(NavigationPropertyConfiguration navigationConfiguration,
            INavigationSourceConfiguration targetNavigationSource)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (targetNavigationSource == null)
            {
                throw Error.ArgumentNull("targetNavigationSource");
            }

            EntityTypeConfiguration declaringEntityType = navigationConfiguration.DeclaringEntityType;
            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyNotInHierarchy,
                    declaringEntityType.FullName, EntityType.FullName, Name);
            }

            NavigationPropertyBindingConfiguration navigationPropertyBinding;
            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBinding))
            {
                if (navigationPropertyBinding.TargetNavigationSource != targetNavigationSource)
                {
                    throw Error.NotSupported(SRResources.RebindingNotSupported);
                }
            }
            else
            {
                navigationPropertyBinding = new NavigationPropertyBindingConfiguration(navigationConfiguration, targetNavigationSource);
                _navigationPropertyBindings[navigationConfiguration] = navigationPropertyBinding;
            }

            return navigationPropertyBinding;
        }
        /// <summary>
        /// Configures the navigation link for the given navigation property for this navigation source.
        /// </summary>
        /// <param name="navigationProperty">The navigation property for which the navigation link is being generated.</param>
        /// <param name="navigationLinkBuilder">The builder used to generate the navigation link.</param>
        /// <returns>Returns itself so that multiple calls can be chained.</returns>
        public virtual INavigationSourceConfiguration HasNavigationPropertyLink(NavigationPropertyConfiguration navigationProperty,
            NavigationLinkBuilder navigationLinkBuilder)
        {
            if (navigationProperty == null)
            {
                throw Error.ArgumentNull("navigationProperty");
            }

            if (navigationLinkBuilder == null)
            {
                throw Error.ArgumentNull("navigationLinkBuilder");
            }

            EntityTypeConfiguration declaringEntityType = navigationProperty.DeclaringEntityType;
            if (!(declaringEntityType.IsAssignableFrom(EntityType) || EntityType.IsAssignableFrom(declaringEntityType)))
            {
                throw Error.Argument("navigationProperty", SRResources.NavigationPropertyNotInHierarchy,
                    declaringEntityType.FullName, EntityType.FullName, Name);
            }

            _navigationPropertyLinkBuilders[navigationProperty] = navigationLinkBuilder;
            return this;
        }
Exemplo n.º 18
0
		private void DoIt(NavigationPropertyConfiguration navProp, EdmEntityType type)
		{
			foreach (var dependentProperty in navProp.DependentProperties)
			{
				if (dependentProperty.GetConfiguration(_configurations).IsIgnored)
				{
					navProp.IsIgnored = true;
					return;
				}
			}
			if (navProp.IsIgnored)
			{
				return;
			}
			EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
			{
				Name = navProp.Name,
				TargetMultiplicity = navProp.Multiplicity,
				Target = GetEdmType(navProp.RelatedClrType) as IEdmEntityType,
				ContainsTarget = navProp.ContainsTarget,
				OnDelete = navProp.OnDeleteAction
			};

			// Principal properties
			if (navProp.PrincipalProperties.Any())
			{
				info.PrincipalProperties = GetDeclaringPropertyInfo(navProp.PrincipalProperties);
			}

			// Dependent properties
			if (navProp.DependentProperties.Any())
			{
				info.DependentProperties = GetDeclaringPropertyInfo(navProp.DependentProperties);
			}

			IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);
			if (navProp.PropertyInfo != null && edmProperty != null)
			{
				_properties[navProp.PropertyInfo] = edmProperty;
			}

			if (edmProperty != null && navProp.IsRestricted)
			{
				_propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
			}
		}
Exemplo n.º 19
0
 /// <summary>
 /// Finds the <see cref="NavigationPropertyBindingConfiguration"/> for the given navigation property.
 /// </summary>
 /// <param name="navigationConfiguration">The navigation property.</param>
 /// <param name="autoCreate">Represents a value specifying if the binding should be created if it is not found.</param>
 /// <returns>The binding if found.</returns>
 public NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                           bool autoCreate)
 {
     return(_configuration.FindBinding(navigationConfiguration, autoCreate));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Finds the bindings <see cref="NavigationPropertyBindingConfiguration"/> for the given navigation property
 /// </summary>
 /// <param name="navigationConfiguration">The navigation property.</param>
 /// <returns>The bindings if found</returns>
 public IEnumerable <NavigationPropertyBindingConfiguration> FindBinding(NavigationPropertyConfiguration navigationConfiguration)
 {
     return(_configuration.FindBinding(navigationConfiguration));
 }
Exemplo n.º 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NavigationPropertyBindingConfiguration"/> class.
 /// </summary>
 /// <param name="navigationProperty">The navigation property for the binding.</param>
 /// <param name="navigationSource">The target navigation source of the binding.</param>
 public NavigationPropertyBindingConfiguration(NavigationPropertyConfiguration navigationProperty,
                                               NavigationSourceConfiguration navigationSource)
     : this(navigationProperty, navigationSource, new object[] { navigationProperty.PropertyInfo })
 {
 }
Exemplo n.º 22
0
 /// <summary>
 /// Finds the <see cref="NavigationPropertyBindingConfiguration"/> for the given navigation property.
 /// </summary>
 /// <param name="navigationConfiguration">The navigation property.</param>
 /// <param name="bindingPath">The navigation binding path.</param>
 /// <returns>The binding if found.</returns>
 public NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                           IList <object> bindingPath)
 {
     return(_configuration.FindBinding(navigationConfiguration, bindingPath));
 }
Exemplo n.º 23
0
        private NavigationPropertyConfiguration AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity, bool containsTarget)
        {
            if (navigationProperty == null)
            {
                throw Error.ArgumentNull("navigationProperty");
            }

            if (!navigationProperty.DeclaringType.IsAssignableFrom(ClrType))
            {
                throw Error.Argument("navigationProperty", SRResources.PropertyDoesNotBelongToType, navigationProperty.Name, ClrType.FullName);
            }

            ValidatePropertyNotAlreadyDefinedInBaseTypes(navigationProperty);
            ValidatePropertyNotAlreadyDefinedInDerivedTypes(navigationProperty);

            PropertyConfiguration propertyConfig;
            NavigationPropertyConfiguration navigationPropertyConfig;
            if (ExplicitProperties.ContainsKey(navigationProperty))
            {
                propertyConfig = ExplicitProperties[navigationProperty];
                if (propertyConfig.Kind != PropertyKind.Navigation)
                {
                    throw Error.Argument("navigationProperty", SRResources.MustBeNavigationProperty, navigationProperty.Name, ClrType.FullName);
                }

                navigationPropertyConfig = propertyConfig as NavigationPropertyConfiguration;
                if (navigationPropertyConfig.Multiplicity != multiplicity)
                {
                    throw Error.Argument("navigationProperty", SRResources.MustHaveMatchingMultiplicity, navigationProperty.Name, multiplicity);
                }
            }
            else
            {
                navigationPropertyConfig = new NavigationPropertyConfiguration(
                    navigationProperty,
                    multiplicity,
                    this);
                if (containsTarget)
                {
                    navigationPropertyConfig = navigationPropertyConfig.Contained();
                }

                ExplicitProperties[navigationProperty] = navigationPropertyConfig;
                // make sure the related type is configured
                ModelBuilder.AddEntityType(navigationPropertyConfig.RelatedClrType);
            }
            return navigationPropertyConfig;
        }
Exemplo n.º 24
0
        /// <summary>
        /// Finds the binding for the given navigation property and tries to create it if it does not exist.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <param name="bindingPath">The binding path.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration,
                                                                          IList <object> bindingPath)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            if (bindingPath == null)
            {
                throw Error.ArgumentNullOrEmpty("bindingPath");
            }

            string path = bindingPath.ConvertBindingPath();

            Dictionary <string, NavigationPropertyBindingConfiguration> navigationPropertyBindings;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBindings))
            {
                NavigationPropertyBindingConfiguration bindingConfiguration;
                if (navigationPropertyBindings.TryGetValue(path, out bindingConfiguration))
                {
                    return(bindingConfiguration);
                }
            }

            if (_modelBuilder.BindingOptions == NavigationPropertyBindingOption.None)
            {
                return(null);
            }

            bool hasSingletonAttribute = navigationConfiguration.PropertyInfo.GetCustomAttributes <SingletonAttribute>().Any();
            Type entityType            = navigationConfiguration.RelatedClrType;

            NavigationSourceConfiguration[] matchedNavigationSources;
            if (hasSingletonAttribute)
            {
                matchedNavigationSources = _modelBuilder.Singletons.Where(es => es.EntityType.ClrType == entityType).ToArray();
            }
            else
            {
                matchedNavigationSources = _modelBuilder.EntitySets.Where(es => es.EntityType.ClrType == entityType).ToArray();
            }

            if (matchedNavigationSources.Length >= 1)
            {
                if (matchedNavigationSources.Length == 1 ||
                    _modelBuilder.BindingOptions == NavigationPropertyBindingOption.Auto)
                {
                    return(AddBinding(navigationConfiguration, matchedNavigationSources[0], bindingPath));
                }

                throw Error.NotSupported(
                          SRResources.CannotAutoCreateMultipleCandidates,
                          path,
                          navigationConfiguration.DeclaringType.FullName,
                          Name,
                          String.Join(", ", matchedNavigationSources.Select(s => s.Name)));
            }

            return(null);
        }
Exemplo n.º 25
0
 /// <summary>
 /// Finds the binding for the given navigation property and tries to create it if it doesnot exist.
 /// </summary>
 /// <param name="navigationConfiguration">The navigation property.</param>
 /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
 public virtual NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration)
 {
     return(FindBinding(navigationConfiguration, autoCreate: true));
 }
        /// <summary>
        /// Finds the binding for the given navigation property.
        /// </summary>
        /// <param name="autoCreate">Tells whether the binding should be auto created if it does not exist.</param>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <returns>The <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual NavigationPropertyBindingConfiguration FindBinding(NavigationPropertyConfiguration navigationConfiguration,
            bool autoCreate)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            NavigationPropertyBindingConfiguration bindingConfiguration;
            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out bindingConfiguration))
            {
                return bindingConfiguration;
            }

            if (!autoCreate)
            {
                return null;
            }

            bool hasSingletonAttribute = navigationConfiguration.PropertyInfo.GetCustomAttributes<SingletonAttribute>().Any();
            Type entityType = navigationConfiguration.RelatedClrType;

            INavigationSourceConfiguration[] matchedNavigationSources;
            if (hasSingletonAttribute)
            {
                matchedNavigationSources = _modelBuilder.Singletons.Where(es => es.EntityType.ClrType == entityType).ToArray();
            }
            else
            {
                matchedNavigationSources = _modelBuilder.EntitySets.Where(es => es.EntityType.ClrType == entityType).ToArray();
            }

            if (matchedNavigationSources.Length == 1)
            {
                return AddBinding(navigationConfiguration, matchedNavigationSources[0]);
            }
            else if (matchedNavigationSources.Length == 0)
            {
                return null;
            }
            else
            {
                throw Error.NotSupported(
                    SRResources.CannotAutoCreateMultipleCandidates,
                    navigationConfiguration.Name,
                    navigationConfiguration.DeclaringEntityType.FullName,
                    Name,
                    String.Join(", ", matchedNavigationSources.Select(s => s.Name)));
            }
        }
        /// <summary>
        /// Gets the builder used to generate navigation link for the given navigation property for this navigation source.
        /// </summary>
        /// <param name="navigationProperty">The navigation property.</param>
        /// <returns>The link builder.</returns>
        public virtual NavigationLinkBuilder GetNavigationPropertyLink(NavigationPropertyConfiguration navigationProperty)
        {
            if (navigationProperty == null)
            {
                throw Error.ArgumentNull("navigationProperty");
            }

            NavigationLinkBuilder navigationPropertyLinkBuilder;
            _navigationPropertyLinkBuilders.TryGetValue(navigationProperty, out navigationPropertyLinkBuilder);
            return navigationPropertyLinkBuilder;
        }
Exemplo n.º 28
0
        /// <summary>
        /// Finds the bindings <see cref="NavigationPropertyBindingConfiguration"/> for the given navigation property.
        /// </summary>
        /// <param name="navigationConfiguration">The navigation property.</param>
        /// <returns>The list of <see cref="NavigationPropertyBindingConfiguration"/> so that it can be further configured.</returns>
        public virtual IEnumerable <NavigationPropertyBindingConfiguration> FindBinding(NavigationPropertyConfiguration navigationConfiguration)
        {
            if (navigationConfiguration == null)
            {
                throw Error.ArgumentNull("navigationConfiguration");
            }

            Dictionary <string, NavigationPropertyBindingConfiguration> navigationPropertyBindings;

            if (_navigationPropertyBindings.TryGetValue(navigationConfiguration, out navigationPropertyBindings))
            {
                return(navigationPropertyBindings.Values);
            }

            return(null);
        }