Example #1
0
        public void Can_add_type_mapped_parameter_by_type(bool nullable)
        {
            var typeMapper = new FakeRelationalTypeMapper();

            var type = nullable
                ? typeof(int?)
                : typeof(int);

            var parameterBuilder = new RelationalParameterBuilder(typeMapper);

            parameterBuilder.AddParameter(
                "InvariantName",
                "Name",
                type,
                unicode: true);

            Assert.Equal(1, parameterBuilder.Parameters.Count);

            var parameter = parameterBuilder.Parameters[0] as TypeMappedRelationalParameter;

            Assert.NotNull(parameter);
            Assert.Equal("InvariantName", parameter.InvariantName);
            Assert.Equal("Name", parameter.Name);
            Assert.Equal(typeMapper.GetMapping(typeof(int)), parameter.RelationalTypeMapping);
            Assert.Equal(nullable, parameter.Nullable);
        }
        public void Can_add_type_mapped_parameter_by_property(bool nullable)
        {
            var typeMapper = new FakeRelationalTypeMapper(new RelationalTypeMapperDependencies());

            var property = new Model().AddEntityType("MyType").AddProperty("MyProp", typeof(string));

            property.IsNullable = nullable;

            var parameterBuilder = new RelationalParameterBuilder(typeMapper);

            parameterBuilder.AddParameter(
                "InvariantName",
                "Name",
                property);

            Assert.Equal(1, parameterBuilder.Parameters.Count);

            var parameter = parameterBuilder.Parameters[0] as TypeMappedRelationalParameter;

            Assert.NotNull(parameter);
            Assert.Equal("InvariantName", parameter.InvariantName);
            Assert.Equal("Name", parameter.Name);
            Assert.Equal(typeMapper.GetMapping(property), parameter.RelationalTypeMapping);
            Assert.Equal(nullable, parameter.IsNullable);
        }
Example #3
0
        public void Can_add_type_mapped_parameter_by_type(bool nullable)
        {
            var typeMapper = new FakeRelationalTypeMapper(
                new CoreTypeMapperDependencies(),
                new RelationalTypeMapperDependencies());
            var typeMapping      = typeMapper.GetMapping(nullable ? typeof(int?) : typeof(int));
            var parameterBuilder = new RelationalParameterBuilder(typeMapper);

            parameterBuilder.AddParameter(
                "InvariantName",
                "Name",
                typeMapping,
                nullable);

            Assert.Equal(1, parameterBuilder.Parameters.Count);

            var parameter = parameterBuilder.Parameters[0] as TypeMappedRelationalParameter;

            Assert.NotNull(parameter);
            Assert.Equal("InvariantName", parameter.InvariantName);
            Assert.Equal("Name", parameter.Name);
            Assert.Equal(typeMapping, parameter.RelationalTypeMapping);
            Assert.Equal(nullable, parameter.IsNullable);
        }
        public async Task Configures_DbCommand_with_dynamic_parameters(
            Delegate commandDelegate,
            string telemetryName,
            bool async)
        {
            var fakeConnection = CreateConnection();

            var typeMapper = new FakeRelationalTypeMapper(new RelationalTypeMapperDependencies());

            var dbParameter = new FakeDbParameter {
                ParameterName = "FirstParameter", Value = 17, DbType = DbType.Int32
            };

            var relationalCommand = CreateRelationalCommand(
                parameters: new[]
            {
                new DynamicRelationalParameter("FirstInvariant", "FirstParameter", typeMapper),
                new DynamicRelationalParameter("SecondInvariant", "SecondParameter", typeMapper),
                new DynamicRelationalParameter("ThirdInvariant", "ThirdParameter", typeMapper)
            });

            var parameterValues = new Dictionary <string, object>
            {
                { "FirstInvariant", dbParameter },
                { "SecondInvariant", 18L },
                { "ThirdInvariant", null }
            };

            if (async)
            {
                await((CommandFunc)commandDelegate)(fakeConnection, relationalCommand, parameterValues);
            }
            else
            {
                ((CommandAction)commandDelegate)(fakeConnection, relationalCommand, parameterValues);
            }

            Assert.Equal(1, fakeConnection.DbConnections.Count);
            Assert.Equal(1, fakeConnection.DbConnections[0].DbCommands.Count);
            Assert.Equal(3, fakeConnection.DbConnections[0].DbCommands[0].Parameters.Count);

            var parameter = fakeConnection.DbConnections[0].DbCommands[0].Parameters[0];

            Assert.Equal(parameter, fakeConnection.DbConnections[0].DbCommands[0].Parameters[0]);

            parameter = fakeConnection.DbConnections[0].DbCommands[0].Parameters[1];
            var mapping = typeMapper.GetMapping(18L.GetType());

            Assert.Equal("SecondParameter", parameter.ParameterName);
            Assert.Equal(18L, parameter.Value);
            Assert.Equal(ParameterDirection.Input, parameter.Direction);
            Assert.Equal(false, parameter.IsNullable);
            Assert.Equal(mapping.DbType, parameter.DbType);

            parameter = fakeConnection.DbConnections[0].DbCommands[0].Parameters[2];

            Assert.Equal("ThirdParameter", parameter.ParameterName);
            Assert.Equal(DBNull.Value, parameter.Value);
            Assert.Equal(ParameterDirection.Input, parameter.Direction);
            Assert.Equal(FakeDbParameter.DefaultIsNullable, parameter.IsNullable);
            Assert.Equal(FakeDbParameter.DefaultDbType, parameter.DbType);
        }