public void HasColumnName_configures_when_unset()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.HasColumnName("Column1");

            Assert.Equal("Column1", innerConfig.ColumnName);
            Assert.Same(config, result);
        }
        public void HasMaxLength_throws_when_not_length()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            Assert.Equal(
               Strings.LightweightPrimitivePropertyConfiguration_NonLength("P"),
               Assert.Throws<InvalidOperationException>(() => config.HasMaxLength(256)).Message);
        }
        public void HasAnnotation_evaluates_preconditions()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("name"),
                Assert.Throws<ArgumentException>(() => config.HasColumnAnnotation(null, null)).Message);

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("name"),
                Assert.Throws<ArgumentException>(() => config.HasColumnAnnotation(" ", null)).Message);

            Assert.Equal(
                Strings.BadAnnotationName("Cheese:Pickle"),
                Assert.Throws<ArgumentException>(() => config.HasColumnAnnotation("Cheese:Pickle", null)).Message);
        }
        public void HasAnnotation_configures_only_annotations_that_have_not_already_been_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            innerConfig.SetAnnotation("A1", "V1");
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.HasColumnAnnotation("A1", "V1B").HasColumnAnnotation("A2", "V2");

            Assert.Equal("V1", innerConfig.Annotations["A1"]);
            Assert.Equal("V2", innerConfig.Annotations["A2"]);
            Assert.Same(config, result);
        }
        internal void Configure(
            DbDatabaseMapping databaseMapping, MappingFragment fragment, EntityType entityType)
        {
            DebugCheck.NotNull(fragment);

            var edmPropertyPath = EntityMappingConfiguration.PropertyPathToEdmPropertyPath(PropertyPath, entityType);

            if (edmPropertyPath.Count() > 1)
            {
                throw Error.InvalidNotNullCondition(PropertyPath.ToString(), entityType.Name);
            }

            var column
                = fragment.ColumnMappings
                          .Where(pm => pm.PropertyPath.SequenceEqual(edmPropertyPath.Single()))
                          .Select(pm => pm.ColumnProperty)
                          .SingleOrDefault();

            if (column == null
                || !fragment.Table.Properties.Contains(column))
            {
                throw Error.InvalidNotNullCondition(PropertyPath.ToString(), entityType.Name);
            }

            if (ValueConditionConfiguration.AnyBaseTypeToTableWithoutColumnCondition(
                databaseMapping, entityType, fragment.Table, column))
            {
                column.Nullable = true;
            }

            // Make the property required
            var newConfiguration = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    IsNullable = false,
                    OverridableConfigurationParts =
                        OverridableConfigurationParts.OverridableInSSpace
                };

            newConfiguration.Configure(edmPropertyPath.Single().Last());

            fragment.AddNullabilityCondition(column, isNull: false);
        }
        public void IsRowVersion_throws_when_not_binary()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            Assert.Equal(
               Strings.LightweightPrimitivePropertyConfiguration_IsRowVersionNonBinary("P"),
               Assert.Throws<InvalidOperationException>(() => config.IsRowVersion()).Message);
        }
        public void IsKey_is_noop_when_set()
        {
            var typeConfig = new EntityTypeConfiguration(typeof(AType2));
            typeConfig.Key(new[] { typeof(AType2).GetDeclaredProperty("Property1") });
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    TypeConfiguration = typeConfig
                };
            var propertyInfo = typeof(AType2).GetDeclaredProperty("Property2");
            var config = new ConventionPrimitivePropertyConfiguration(propertyInfo, () => innerConfig);

            var result = config.IsKey();

            Assert.DoesNotContain(propertyInfo, typeConfig.KeyProperties);
            Assert.Same(config, result);
        }
        public void IsConcurrencyToken_with_parameter_is_noop_when_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    ConcurrencyMode = ConcurrencyMode.Fixed
                };
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.IsConcurrencyToken(false);

            Assert.Equal(ConcurrencyMode.Fixed, innerConfig.ConcurrencyMode);
            Assert.Same(config, result);
        }
        public void HasDatabaseGeneratedOption_evaluates_preconditions()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var ex = Assert.Throws<ArgumentOutOfRangeException>(
                () => config.HasDatabaseGeneratedOption((DatabaseGeneratedOption)(-1)));

            Assert.Equal("databaseGeneratedOption", ex.ParamName);
        }
        public void HasColumnType_is_noop_when_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    ColumnType = "int"
                };
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.HasColumnType("long");

            Assert.Equal("int", innerConfig.ColumnType);
            Assert.Same(config, result);
        }
        public void IsConcurrencyToken_configures_when_unset()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.IsConcurrencyToken();

            Assert.Equal(ConcurrencyMode.Fixed, innerConfig.ConcurrencyMode);
            Assert.Same(config, result);
        }
        public void HasColumnOrder_is_noop_when_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    ColumnOrder = 1
                };
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.HasColumnOrder(2);

            Assert.Equal(1, innerConfig.ColumnOrder);
            Assert.Same(config, result);
        }
        public void HasColumnOrder_throws_on_negative_arguments()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            Assert.Equal(
                "columnOrder",
                Assert.Throws<ArgumentOutOfRangeException>(() => config.HasColumnOrder(-1)).ParamName);
        }
        public void HasParameterName_is_noop_when_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    ParameterName = "Parameter1"
                };
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.HasParameterName("Parameter2");

            Assert.Equal("Parameter1", innerConfig.ParameterName);
            Assert.Same(config, result);
        }
        public void HasPrecision_with_scale_throws_when_not_decimal()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            Assert.Equal(
                Strings.LightweightPrimitivePropertyConfiguration_HasPrecisionNonDecimal("P"),
                Assert.Throws<InvalidOperationException>(() => config.HasPrecision(8, 2)).Message);
        }
        public void HasDatabaseGeneratedOption_is_noop_when_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    DatabaseGeneratedOption = DatabaseGeneratedOption.Computed
                };
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            Assert.Equal(DatabaseGeneratedOption.Computed, innerConfig.DatabaseGeneratedOption);
            Assert.Same(config, result);
        }
        public void HasColumnName_evaluates_preconditions()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var ex = Assert.Throws<ArgumentException>(
                () => config.HasColumnName(""));

            Assert.Equal(Strings.ArgumentIsNullOrWhitespace("columnName"), ex.Message);
        }
        public void IsOptional_configures_when_unset()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.IsOptional();

            Assert.Equal(true, innerConfig.IsNullable);
            Assert.Same(config, result);
        }
        public void IsKey_configures_when_unset()
        {
            var typeConfig = new EntityTypeConfiguration(typeof(AType1));
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    TypeConfiguration = typeConfig
                };
            var propertyInfo = typeof(AType1).GetDeclaredProperty("Property1");
            var config = new ConventionPrimitivePropertyConfiguration(propertyInfo, () => innerConfig);

            var result = config.IsKey();

            Assert.Equal(1, typeConfig.KeyProperties.Count());
            Assert.Contains(propertyInfo, typeConfig.KeyProperties);
            Assert.Same(config, result);
        }
        public void IsOptional_throws_on_non_nullable_property()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(typeof(int), "IntProperty"), () => innerConfig);

            Assert.Equal(
                Strings.LightweightPrimitivePropertyConfiguration_NonNullableProperty("System.Object.IntProperty", typeof(int).Name),
                Assert.Throws<InvalidOperationException>(() => config.IsOptional()).Message);
        }
        public void ClrPropertyInfo_returns_propertyInfo()
        {
            var propertyInfo = new MockPropertyInfo();
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(propertyInfo, () => innerConfig);

            Assert.Same(propertyInfo.Object, config.ClrPropertyInfo);
        }
        public void IsRequired_is_noop_when_set()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration
                {
                    IsNullable = true
                };
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var result = config.IsRequired();

            Assert.Equal(true, innerConfig.IsNullable);
            Assert.Same(config, result);
        }
        public void Configure_should_preserve_the_most_derived_new_configuration()
        {
            var configurationA = CreateConfiguration();
            configurationA.ConcurrencyMode = ConcurrencyMode.Fixed;
            var configurationB = new Properties.Primitive.PrimitivePropertyConfiguration();
            configurationB.IsNullable = false;

            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));

            Assert.Null(property.GetConfiguration());

            configurationB.Configure(property);
            configurationA.Configure(property);

            Assert.Equal(
                ConcurrencyMode.Fixed, ((Properties.Primitive.PrimitivePropertyConfiguration)property.GetConfiguration()).ConcurrencyMode);
            Assert.Equal(false, ((Properties.Primitive.PrimitivePropertyConfiguration)property.GetConfiguration()).IsNullable);
            Assert.Equal(GetConfigurationType(), property.GetConfiguration().GetType());
        }
        public void IsUnicode_with_parameter_throws_when_not_string()
        {
            var innerConfig = new Properties.Primitive.PrimitivePropertyConfiguration();
            var config = new ConventionPrimitivePropertyConfiguration(new MockPropertyInfo(), () => innerConfig);

            var ex = Assert.Throws<InvalidOperationException>(() => config.IsUnicode(false));

            Assert.Equal(Strings.LightweightPrimitivePropertyConfiguration_IsUnicodeNonString("P"), ex.Message);
        }
        internal PropertyMappingConfiguration(Properties.Primitive.PrimitivePropertyConfiguration configuration)
        {
            DebugCheck.NotNull(configuration);

            _configuration = configuration;
        }
        internal PrimitiveColumnConfiguration(Properties.Primitive.PrimitivePropertyConfiguration configuration)
        {
            //Contract.Requires(configuration != null);

            _configuration = configuration;
        }