public void Apply_should_discover_for_self_reference()
        {
            var associationType = new EdmAssociationType().Initialize();

            associationType.SourceEnd.EndKind = EdmAssociationEndKind.Optional;
            associationType.SourceEnd.EntityType = new EdmEntityType();

            associationType.TargetEnd.EndKind = EdmAssociationEndKind.Many;
            associationType.TargetEnd.EntityType = associationType.SourceEnd.EntityType;

            var pkProperty = new EdmProperty().AsPrimitive();
            associationType.SourceEnd.EntityType.DeclaredKeyProperties.Add(pkProperty);

            var fkProperty = new EdmProperty().AsPrimitive();
            associationType.TargetEnd.EntityType.DeclaredProperties.Add(fkProperty);
            associationType.TargetEnd.EntityType.AddNavigationProperty("Nav", associationType).ResultEnd = associationType.SourceEnd;
            associationType.TargetEnd.EntityType.AddNavigationProperty("Foos", associationType);

            // Foo.Id == Foo.NavId
            pkProperty.Name = "Id";
            fkProperty.Name = "NavId";

            ((IEdmConvention<EdmAssociationType>)new NavigationPropertyNameForeignKeyDiscoveryConvention())
                .Apply(associationType, new EdmModel().Initialize());

            Assert.NotNull(associationType.Constraint);
            Assert.Same(associationType.TargetEnd, associationType.Constraint.DependentEnd);
            Assert.Equal("NavId", associationType.Constraint.DependentProperties.Single().Name);
        }
        public void AsPrimitive_should_create_property_type_and_facets()
        {
            var property = new EdmProperty().AsPrimitive();

            Assert.NotNull(property.PropertyType);
            Assert.NotNull(property.PropertyType.PrimitiveTypeFacets);
        }
        public void AsEnum_should_create_property_type()
        {
            var property = new EdmProperty().AsEnum(new EdmEnumType());

            Assert.NotNull(property.PropertyType);
            Assert.True(property.PropertyType.IsEnumType);
        }
        public void Apply_should_discover_composite_matching_foreign_keys()
        {
            var associationType = CreateAssociationType();

            var pkProperty1 = new EdmProperty().AsPrimitive();
            var pkProperty2 = new EdmProperty().AsPrimitive();
            associationType.SourceEnd.EntityType.DeclaredKeyProperties.Add(pkProperty1);
            associationType.SourceEnd.EntityType.DeclaredKeyProperties.Add(pkProperty2);

            var fkProperty1 = new EdmProperty().AsPrimitive();
            var fkProperty2 = new EdmProperty().AsPrimitive();
            associationType.TargetEnd.EntityType.DeclaredProperties.Add(fkProperty1);
            associationType.TargetEnd.EntityType.DeclaredProperties.Add(fkProperty2);

            // Foo.PId1 == Bar.PId1 && Foo.PId2 == Bar.PId2
            pkProperty1.Name = "PId1";
            pkProperty2.Name = "PId2";
            fkProperty1.Name = "PId1";
            fkProperty2.Name = "PId2";

            ((IEdmConvention<EdmAssociationType>)new PrimaryKeyNameForeignKeyDiscoveryConvention())
                .Apply(associationType, new EdmModel().Initialize());

            Assert.NotNull(associationType.Constraint);
            Assert.Same(associationType.TargetEnd, associationType.Constraint.DependentEnd);
            Assert.Equal(2, associationType.Constraint.DependentProperties.Count());
        }
        public void Can_get_and_set_configuration_annotation()
        {
            var property = new EdmProperty();

            property.SetConfiguration(42);

            Assert.Equal(42, property.GetConfiguration());
        }
        public void AsComplex_should_create_property_type()
        {
            var property = new EdmProperty().AsComplex(new EdmComplexType());

            Assert.NotNull(property.PropertyType);
            Assert.True(property.PropertyType.IsComplexType);
            Assert.Equal(false, property.PropertyType.IsNullable);
        }
        public void GetStoreGeneratedPattern_should_return_null_when_not_set()
        {
            var property = new EdmProperty().AsPrimitive();

            var storeGeneratedPattern = property.GetStoreGeneratedPattern();

            Assert.Null(storeGeneratedPattern);
        }
        private EdmProperty MapPrimitiveOrComplexOrEnumProperty(
            PropertyInfo propertyInfo, Func<StructuralTypeConfiguration> structuralTypeConfiguration,
            bool discoverComplexTypes = false)
        {
            //Contract.Requires(propertyInfo != null);

            var property = propertyInfo.AsEdmPrimitiveProperty();

            if (property == null)
            {
                var propertyType = propertyInfo.PropertyType;
                var complexType = _typeMapper.MapComplexType(propertyType, discoverComplexTypes);

                if (complexType != null)
                {
                    property = new EdmProperty
                        {
                            Name = propertyInfo.Name
                        }.AsComplex(complexType);
                }
                else
                {
                    var isNullable = propertyType.TryUnwrapNullableType(out propertyType);

                    if (propertyType.IsEnum)
                    {
                        var enumType = _typeMapper.MapEnumType(propertyType);

                        if (enumType != null)
                        {
                            property = new EdmProperty
                                {
                                    Name = propertyInfo.Name,
                                }.AsEnum(enumType);
                            property.PropertyType.IsNullable = isNullable;
                        }
                    }
                }
            }

            if (property != null)
            {
                property.SetClrPropertyInfo(propertyInfo);

                new AttributeMapper(_typeMapper.MappingContext.AttributeProvider)
                    .Map(propertyInfo, property.Annotations);

                if (!property.PropertyType.IsComplexType)
                {
                    _typeMapper.MappingContext.ConventionsConfiguration.ApplyPropertyConfiguration(
                        propertyInfo, () => structuralTypeConfiguration().Property(new PropertyPath(propertyInfo)));
                }
            }

            return property;
        }
        public void Configure_should_update_model_dateTime_precision()
        {
            var configuration = CreateConfiguration();
            configuration.Precision = 255;
            var property = new EdmProperty().AsPrimitive();

            configuration.Configure(property);

            Assert.Equal((byte)255, property.PropertyType.PrimitiveTypeFacets.Precision);
        }
        public void Configure_should_update_IsUnicode()
        {
            var configuration = CreateConfiguration();
            configuration.IsUnicode = true;
            var property = new EdmProperty().AsPrimitive();

            configuration.Configure(property);

            Assert.Equal(true, property.PropertyType.PrimitiveTypeFacets.IsUnicode);
        }
        public void SetStoreGeneratedPattern_should_create_annotation_and_add_to_property_facets()
        {
            var property = new EdmProperty().AsPrimitive();

            property.SetStoreGeneratedPattern(DbStoreGeneratedPattern.Computed);

            var storeGeneratedPattern = property.GetStoreGeneratedPattern();

            Assert.NotNull(storeGeneratedPattern);
            Assert.Equal(DbStoreGeneratedPattern.Computed, storeGeneratedPattern);
        }
        public void Configure_should_update_model_decimal_scale_but_not_precision()
        {
            var configuration = CreateConfiguration();
            configuration.Scale = 70;
            var property = new EdmProperty().AsPrimitive();

            configuration.Configure(property);

            Assert.Equal((byte)70, property.PropertyType.PrimitiveTypeFacets.Scale);
            Assert.Equal(null, property.PropertyType.PrimitiveTypeFacets.Precision);
        }
        public void Configure_should_update_MaxLength()
        {
            var property = new EdmProperty().AsPrimitive();

            var configuration = CreateConfiguration();
            configuration.MaxLength = 1;

            configuration.Configure(property);

            Assert.Equal(1, property.PropertyType.PrimitiveTypeFacets.MaxLength);
        }
        public void Configure_should_set_CSpace_configuration_annotation()
        {
            var configuration = CreateConfiguration();
            var property = new EdmProperty().AsPrimitive();

            Assert.Null(property.GetConfiguration());

            configuration.Configure(property);

            Assert.Same(configuration, property.GetConfiguration());
        }
        public void SetStoreGeneratedPattern_should_update_existing_annotation()
        {
            var property = new EdmProperty().AsPrimitive();

            property.SetStoreGeneratedPattern(DbStoreGeneratedPattern.Computed);
            property.SetStoreGeneratedPattern(DbStoreGeneratedPattern.None);

            var storeGeneratedPattern = property.GetStoreGeneratedPattern();

            Assert.NotNull(storeGeneratedPattern);
            Assert.Equal(DbStoreGeneratedPattern.None, storeGeneratedPattern);
        }
        public void Apply_should_set_correct_defaults_for_unconfigured_decimal()
        {
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.Decimal;

            ((IEdmConvention<EdmProperty>)new DecimalPropertyConvention())
                .Apply(property, new EdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal((byte)18, primitiveTypeFacets.Precision);
            Assert.Equal((byte)2, primitiveTypeFacets.Scale);
        }
        public static EdmProperty AddPrimitiveProperty(this EdmComplexType complexType, string name)
        {
            //Contract.Requires(complexType != null);
            //Contract.Requires(complexType.Properties != null);
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));

            var property = new EdmProperty().AsPrimitive();
            property.Name = name;

            complexType.DeclaredProperties.Add(property);

            return property;
        }
        public void Apply_should_set_correct_defaults_for_unconfigured_strings()
        {
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.String;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new SqlCePropertyMaxLengthConvention())
                .Apply(entityType, CreateEdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal(4000, primitiveTypeFacets.MaxLength);
        }
        public void Apply_should_match_simple_short_key()
        {
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.Int16;
            entityType.DeclaredKeyProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, new EdmModel().Initialize());

            Assert.Equal(
                DbStoreGeneratedPattern.Identity,
                entityType.DeclaredKeyProperties.Single().GetStoreGeneratedPattern());
        }
        internal override void Configure(EdmProperty property)
        {
            if (IsRowVersion != null)
            {
                ColumnType = ColumnType ?? "rowversion";
                ConcurrencyMode = ConcurrencyMode ?? EdmConcurrencyMode.Fixed;
                DatabaseGeneratedOption
                    = DatabaseGeneratedOption
                      ?? ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed;
                IsNullable = IsNullable ?? false;
                MaxLength = MaxLength ?? 8;
            }

            base.Configure(property);
        }
        public static EdmProperty AddComplexProperty(
            this EdmComplexType complexType, string name, EdmComplexType targetComplexType)
        {
            //Contract.Requires(complexType != null);
            //Contract.Requires(complexType.Properties != null);
            //Contract.Requires(!string.IsNullOrWhiteSpace(name));
            //Contract.Requires(targetComplexType != null);

            var property = new EdmProperty
                {
                    Name = name
                }.AsComplex(targetComplexType);

            complexType.DeclaredProperties.Add(property);

            return property;
        }
        public void Apply_should_set_correct_defaults_for_non_unicode_fixed_length_strings()
        {
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();
            property.PropertyType.EdmType = EdmPrimitiveType.String;
            property.PropertyType.PrimitiveTypeFacets.IsFixedLength = true;
            property.PropertyType.PrimitiveTypeFacets.IsUnicode = false;
            entityType.DeclaredProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new PropertyMaxLengthConvention())
                .Apply(entityType, new EdmModel());

            var primitiveTypeFacets = property.PropertyType.PrimitiveTypeFacets;

            Assert.Equal(128, primitiveTypeFacets.MaxLength);
            Assert.Equal(null, primitiveTypeFacets.IsMaxLength);
        }
        public void Configure_should_update_IsRowVersion()
        {
            var configuration = CreateConfiguration();
            configuration.IsRowVersion = true;
            var property = new EdmProperty().AsPrimitive();

            configuration.Configure(property);

            Assert.Equal(8, property.PropertyType.PrimitiveTypeFacets.MaxLength);
            Assert.Equal(false, property.PropertyType.IsNullable);
            Assert.Equal(EdmConcurrencyMode.Fixed, property.ConcurrencyMode);
            Assert.Equal(DbStoreGeneratedPattern.Computed, property.GetStoreGeneratedPattern());

            var edmPropertyMapping = new DbEdmPropertyMapping { Column = new DbTableColumnMetadata { Facets = new DbPrimitiveTypeFacets() } };

            configuration.Configure(new[] { Tuple.Create(edmPropertyMapping, new DbTableMetadata()) }, ProviderRegistry.Sql2008_ProviderManifest);
            Assert.Equal("rowversion", edmPropertyMapping.Column.TypeName);
        }
        public void GetPropertyMapping_should_return_mapping_with_path()
        {
            var entityTypeMapping = new DbEntityTypeMapping();
            var propertyFoo = new EdmProperty { Name = "Foo" };
            var propertyBar = new EdmProperty { Name = "Bar" };
            var entityPropertyMapping = new DbEdmPropertyMapping
                {
                    PropertyPath = new[]
                        {
                            propertyFoo,
                            propertyBar,
                        }
                };

            var entityTypeMappingFragment = new DbEntityTypeMappingFragment();
            entityTypeMappingFragment.PropertyMappings.Add(entityPropertyMapping);
            entityTypeMapping.TypeMappingFragments.Add(entityTypeMappingFragment);

            Assert.Same(entityPropertyMapping, entityTypeMapping.GetPropertyMapping(propertyFoo, propertyBar));
        }
        public void Configure_should_merge_CSpace_configurations()
        {
            var configurationA = CreateConfiguration();
            configurationA.ConcurrencyMode = EdmConcurrencyMode.Fixed;
            var configurationB = CreateConfiguration();
            configurationB.IsNullable = false;

            var property = new EdmProperty().AsPrimitive();

            Assert.Null(property.GetConfiguration());

            configurationA.Configure(property);

            Assert.Equal(EdmConcurrencyMode.Fixed, ((PrimitivePropertyConfiguration)property.GetConfiguration()).ConcurrencyMode);

            configurationB.Configure(property);

            Assert.Equal(EdmConcurrencyMode.Fixed, ((PrimitivePropertyConfiguration)property.GetConfiguration()).ConcurrencyMode);
            Assert.Equal(false, ((PrimitivePropertyConfiguration)property.GetConfiguration()).IsNullable);
        }
        public void Apply_should_discover_simple_matching_foreign_key_with_different_casing()
        {
            var associationType = CreateAssociationType();

            var pkProperty = new EdmProperty().AsPrimitive();
            associationType.SourceEnd.EntityType.DeclaredKeyProperties.Add(pkProperty);

            var fkProperty = new EdmProperty().AsPrimitive();
            associationType.TargetEnd.EntityType.DeclaredProperties.Add(fkProperty);

            // Foo.PID == Bar.PId
            pkProperty.Name = "PID";
            fkProperty.Name = "PId";

            ((IEdmConvention<EdmAssociationType>)new PrimaryKeyNameForeignKeyDiscoveryConvention())
                .Apply(associationType, new EdmModel().Initialize());

            Assert.NotNull(associationType.Constraint);
            Assert.Same(associationType.TargetEnd, associationType.Constraint.DependentEnd);
            Assert.Equal("PId", associationType.Constraint.DependentProperties.Single().Name);
        }
        public void Apply_should_not_match_key_that_is_also_an_fk()
        {
            var model = new EdmModel().Initialize();
            var entityType = new EdmEntityType();
            var property = new EdmProperty().AsPrimitive();

            property.PropertyType.EdmType = EdmPrimitiveType.Int64;
            entityType.DeclaredKeyProperties.Add(property);

            var associationType
                = model.AddAssociationType(
                    "A", new EdmEntityType(), EdmAssociationEndKind.Optional,
                    entityType, EdmAssociationEndKind.Many);
            associationType.Constraint = new EdmAssociationConstraint();
            associationType.Constraint.DependentProperties.Add(property);

            ((IEdmConvention<EdmEntityType>)new StoreGeneratedIdentityKeyConvention())
                .Apply(entityType, model);

            Assert.Null(entityType.DeclaredKeyProperties.Single().GetStoreGeneratedPattern());
        }
        public void Configure_should_preserve_the_most_derived_configuration()
        {
            var configurationA = CreateConfiguration();
            configurationA.ConcurrencyMode = EdmConcurrencyMode.Fixed;
            var configurationB = new PrimitivePropertyConfiguration();
            configurationB.IsNullable = false;

            var property = new EdmProperty().AsPrimitive();

            Assert.Null(property.GetConfiguration());

            configurationA.Configure(property);

            Assert.Equal(EdmConcurrencyMode.Fixed, ((PrimitivePropertyConfiguration)property.GetConfiguration()).ConcurrencyMode);

            configurationB.Configure(property);

            Assert.Equal(EdmConcurrencyMode.Fixed, ((PrimitivePropertyConfiguration)property.GetConfiguration()).ConcurrencyMode);
            Assert.Equal(false, ((PrimitivePropertyConfiguration)property.GetConfiguration()).IsNullable);
            Assert.Equal(GetConfigurationType(), property.GetConfiguration().GetType());
        }
        public void Apply_should_discover_simple_matching_foreign_key()
        {
            var associationType = CreateAssociationType();

            var pkProperty = new EdmProperty().AsPrimitive();
            associationType.SourceEnd.EntityType.DeclaredKeyProperties.Add(pkProperty);

            var fkProperty = new EdmProperty().AsPrimitive();
            associationType.TargetEnd.EntityType.DeclaredProperties.Add(fkProperty);
            associationType.TargetEnd.EntityType.AddNavigationProperty("Nav", associationType).ResultEnd = associationType.SourceEnd;

            // Foo.Id == Bar.NavId
            pkProperty.Name = "Id";
            fkProperty.Name = "NavId";

            ((IEdmConvention<EdmAssociationType>)new NavigationPropertyNameForeignKeyDiscoveryConvention())
                .Apply(associationType, new EdmModel().Initialize());

            Assert.NotNull(associationType.Constraint);
            Assert.Same(associationType.TargetEnd, associationType.Constraint.DependentEnd);
            Assert.Equal("NavId", associationType.Constraint.DependentProperties.Single().Name);
        }
        protected void MapTableColumn(
            EdmProperty property,
            DbTableColumnMetadata tableColumnMetadata,
            bool isInstancePropertyOnDerivedType,
            bool isKeyProperty = false)
        {
            Contract.Requires(property != null);
            Contract.Requires(tableColumnMetadata != null);

            var storeTypeUsage = _providerManifest.GetStoreType(GetEdmTypeUsage(property.PropertyType));

            tableColumnMetadata.TypeName = storeTypeUsage.EdmType.Name;
            tableColumnMetadata.IsPrimaryKeyColumn = isKeyProperty;

            if (isInstancePropertyOnDerivedType)
            {
                tableColumnMetadata.IsNullable = true;
            }
            else if (property.PropertyType.IsNullable != null)
            {
                tableColumnMetadata.IsNullable = property.PropertyType.IsNullable.Value;
            }

            if (tableColumnMetadata.IsPrimaryKeyColumn)
            {
                tableColumnMetadata.IsNullable = false;
            }

            var storeGeneratedPattern = property.GetStoreGeneratedPattern();

            if (storeGeneratedPattern != null)
            {
                tableColumnMetadata.StoreGeneratedPattern = storeGeneratedPattern.Value;
            }

            MapPrimitivePropertyFacets(
                property.PropertyType.PrimitiveTypeFacets, tableColumnMetadata.Facets, storeTypeUsage);
        }