public void Can_get_and_set_configuration_facet()
        {
            var navigationProperty = new EdmNavigationProperty();
            navigationProperty.SetConfiguration(42);

            Assert.Equal(42, navigationProperty.GetConfiguration());
        }
        public void Configure_should_set_configuration_annotations()
        {
            var navigationPropertyConfiguration = new NavigationPropertyConfiguration(new MockPropertyInfo());
            var navigationProperty = new EdmNavigationProperty { Association = new EdmAssociationType().Initialize() };

            navigationPropertyConfiguration.Configure(navigationProperty, new EdmModel(), new EntityTypeConfiguration(typeof(object)));

            Assert.NotNull(navigationProperty.GetConfiguration());
            Assert.NotNull(navigationProperty.Association.GetConfiguration());
        }
        public void Apply_is_noop_when_no_fk_annotation()
        {
            var navigationProperty = new EdmNavigationProperty
                {
                    Association = new EdmAssociationType()
                };

            ((IEdmConvention<EdmNavigationProperty>)new ForeignKeyNavigationPropertyAttributeConvention())
                .Apply(navigationProperty, new EdmModel());

            Assert.Null(navigationProperty.Association.Constraint);
        }
        public void Apply_is_noop_when_unknown_dependent()
        {
            var model = new EdmModel().Initialize();
            var associationType = new EdmAssociationType().Initialize();
            var navigationProperty = new EdmNavigationProperty { Association = associationType };
            var foreignKeyAnnotation = new ForeignKeyAttribute("AId");
            navigationProperty.Annotations.SetClrAttributes(new[] { foreignKeyAnnotation });

            ((IEdmConvention<EdmNavigationProperty>)new ForeignKeyNavigationPropertyAttributeConvention())
                .Apply(navigationProperty, model);

            Assert.Null(associationType.Constraint);
        }
        public void Apply_is_noop_when_existing_constraint()
        {
            var associationConstraint = new EdmAssociationConstraint();
            var navigationProperty = new EdmNavigationProperty
                {
                    Association = new EdmAssociationType
                        {
                            Constraint = associationConstraint
                        }
                };

            ((IEdmConvention<EdmNavigationProperty>)new ForeignKeyNavigationPropertyAttributeConvention())
                .Apply(navigationProperty, new EdmModel());

            Assert.Same(associationConstraint, navigationProperty.Association.Constraint);
        }
        internal void Configure(
            EdmNavigationProperty navigationProperty, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
        {
            //Contract.Requires(navigationProperty != null);
            //Contract.Requires(model != null);
            //Contract.Requires(entityTypeConfiguration != null);

            navigationProperty.SetConfiguration(this);

            var associationType = navigationProperty.Association;
            var configuration = associationType.GetConfiguration() as NavigationPropertyConfiguration;

            if (configuration == null)
            {
                associationType.SetConfiguration(this);
            }
            else
            {
                ValidateConsistency(configuration);
            }

            ConfigureInverse(associationType, model);
            ConfigureEndKinds(associationType, configuration);
            ConfigureDependentBehavior(associationType, model, entityTypeConfiguration);
        }
        public static EdmNavigationProperty AddNavigationProperty(
            this EdmEntityType entityType, string name, EdmAssociationType associationType)
        {
            //Contract.Requires(entityType != null);
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));
            //Contract.Requires(associationType != null);

            var navigationProperty = new EdmNavigationProperty
                {
                    Name = name,
                    Association = associationType,
                    ResultEnd = associationType.TargetEnd
                };

            entityType.DeclaredNavigationProperties.Add(navigationProperty);

            return navigationProperty;
        }
        private void GenerateIndependentForeignKeyColumns(
            EdmEntityType principalEntityType,
            EdmEntityType dependentEntityType,
            DbAssociationSetMapping associationSetMapping,
            DbAssociationEndMapping associationEndMapping,
            DbTableMetadata dependentTable,
            DbForeignKeyConstraintMetadata foreignKeyConstraint,
            bool isPrimaryKeyColumn,
            EdmNavigationProperty principalNavigationProperty)
        {
            //Contract.Requires(principalEntityType != null);
            //Contract.Requires(associationEndMapping != null);
            //Contract.Requires(dependentTable != null);
            //Contract.Requires(foreignKeyConstraint != null);

            foreach (var property in principalEntityType.KeyProperties())
            {
                var foreignKeyColumn
                    = dependentTable.AddColumn(
                        ((principalNavigationProperty != null)
                             ? principalNavigationProperty.Name
                             : principalEntityType.Name)
                        + "_" + property.Name);

                MapTableColumn(property, foreignKeyColumn, false, isPrimaryKeyColumn);

                foreignKeyColumn.IsNullable = (associationEndMapping.AssociationEnd.IsOptional()
                                               ||
                                               (associationEndMapping.AssociationEnd.IsRequired()
                                                && dependentEntityType.BaseType != null));
                foreignKeyColumn.StoreGeneratedPattern = DbStoreGeneratedPattern.None;

                foreignKeyConstraint.DependentColumns.Add(foreignKeyColumn);

                associationEndMapping.PropertyMappings.Add(
                    new DbEdmPropertyMapping
                        {
                            Column = foreignKeyColumn,
                            PropertyPath = new[] { property }
                        });

                if (foreignKeyColumn.IsNullable)
                {
                    associationSetMapping.ColumnConditions.Add(
                        new DbColumnCondition
                            {
                                Column = foreignKeyColumn,
                                IsNull = false
                            });
                }
            }
        }