Пример #1
0
        public void TestInitialize()
        {
            this.Connection = new SqlConnection("Data Source=(LocalDb)\v11.0;Initial Catalog=tempdb;Integrated Security=SSPI;");
            this.Connection.Open();

            var mapper = new DbClassMapBuilder<TestEntity>();
            mapper.Property(x => x.Int32Value);
            mapper.Property(x => x.StringValue);
            mapper.Property(x => x.NullableInt32Value);
            mapper.Property(x => x.NullableDoubleValue);
            mapper.Property(x => x.IntToEnumValue);
            mapper.Property(x => x.StringToEnumValue)
                  .HasColumnReader(DbColumnReader.StringToEnum<TestEntityEnum>());
            mapper.Property(x => x.ParsedProperty)
                  .HasColumnReader((x, i) => (x.GetStringOrDefault(i) ?? string.Empty).Split(','));
            mapper.Property(x => x.DefaultNameValue);
            DefaultMap = mapper.CreateMap();
        }
Пример #2
0
 public void Should_read_case_insensitive()
 {
     var mapBuilder = new DbClassMapBuilder<TestEntity>(DbClassMap<TestEntity>.Default);
     mapBuilder.Property(x => x.StringValue)
               .HasColumnName("MappedValue");
     var map = mapBuilder.CreateMap();
     var result = Connection.Entity(map)
                            .SqlQuery("SELECT 1 As INT32VALUE, 'Hello World' As MAPPEDVALUE").Single();
     Assert.AreEqual(1, result.Int32Value);
     Assert.AreEqual("Hello World", result.StringValue);
 }
Пример #3
0
 public void Should_customize_default_map()
 {
     // override just id
     var mapBuilder = new DbClassMapBuilder<TestEntity>(DbClassMap<TestEntity>.Default);
     mapBuilder.Property(x => x.Int32Value)
               .HasColumnName("Id");
     var map = mapBuilder.CreateMap();
     var result = Connection.Entity(map).SqlQuery("SELECT 1 AS Id, 'Test' AS StringValue").Single();
     Assert.AreEqual(1, result.Int32Value);
     Assert.AreEqual("Test", result.StringValue);
 }
Пример #4
0
        public void Should_allow_non_static_custom_setter()
        {
            var mapBuilder = new DbClassMapBuilder<TestEntity>();
            mapBuilder.Property(x => x.Int32Value);
            mapBuilder.Property(x => x.CustomSetterValue)
                      .HasPropertySetter((x, value) =>
                          {
                              x.CustomSetterValue = value;
                              x.CustomSetterValueSpecified = true;
                          });
            var map = mapBuilder.CreateMap();

            // verify value read
            var result = Connection.Entity(map).SqlQuery("SELECT 1 As CustomSetterValue").Single();
            Assert.AreEqual(1, result.CustomSetterValue);
            Assert.IsTrue(result.CustomSetterValueSpecified);

            // verify value not read
            result = Connection.Entity(map).SqlQuery("SELECT 1 As Int32Value").Single();
            Assert.AreEqual(0, result.CustomSetterValue);
            Assert.IsFalse(result.CustomSetterValueSpecified);
        }
Пример #5
0
 public void Should_allow_static_custom_setter()
 {
     var mapBuilder = new DbClassMapBuilder<TestEntity>();
     mapBuilder.Property(x => x.CustomSetterValue)
               .HasPropertySetter((x, v) =>
                   {
                       x.CustomSetterValue = v;
                       x.CustomSetterValueSpecified = true;
                   });
     var map = mapBuilder.CreateMap();
     var results = Connection.Entity(map).SqlQuery("SELECT 1 As CustomSetterValue")
                             .ToArray();
     Assert.AreEqual(1, results[0].CustomSetterValue);
     Assert.IsTrue(results[0].CustomSetterValueSpecified);
 }
Пример #6
0
        public void Should_allow_complex_content()
        {
            var mapBuilder = new DbClassMapBuilder<TestEntity>();
            mapBuilder.Property(x => x.Int32Value)
                      .HasColumnName("Id");
            mapBuilder.Property(x => x.StringValue)
                      .HasColumnName("Value");
            var map = mapBuilder.CreateMap();

            const string SQL = @"
                DECLARE @TestTable TABLE (Id INT, Value NVARCHAR(50))
                INSERT INTO @TestTable VALUES (@InputId, @InputValue)
                SELECT * FROM @TestTable";
            var results = Connection.ExecuteReader(SQL, r => new DbObjectReader<TestEntity>(r, map), new
                {
                    InputId = 1,
                    InputValue = "Hello World"
                }).ToArray();
            Assert.AreEqual(1, results[0].Int32Value);
            Assert.AreEqual("Hello World", results[0].StringValue);
        }