Exemplo n.º 1
0
        public void Inserting_Data_Works()
        {
            using (_Connection)
            {
                List <Person> people = new List <Person>()
                {
                    new Person()
                    {
                        Id = 101, Name = "InsertTest-1", IsMarried = false, DateOfBirth = new DateTime(1985, 2, 17), PlaceOfBirth = "Zurich",
                    },
                    new Person()
                    {
                        Id = 102, Name = "InsertTest-2", IsMarried = true, DateOfBirth = new DateTime(1972, 11, 2), PlaceOfBirth = "London",
                    },
                    new Person()
                    {
                        Id = 103, Name = "InsertTest-3", IsMarried = false, DateOfBirth = new DateTime(1953, 8, 15), PlaceOfBirth = "New York",
                    },
                };

                // check
                var checkCmd = _Connection.CreateCommand();
                checkCmd.CommandText = @"SELECT MAX(Id) FROM People ";
                int maxId = (int)checkCmd.ExecuteScalar();

                InsertCommand <Person> cmd = new InsertCommand <Person>(_Connection, "People");
                cmd.CreateDynamicMappings(new string[] { "Id" });
                cmd.MapIdentity((p, val) => p.Id = Convert.ToInt32(val));
                cmd.Execute(people);

                Assert.AreEqual(maxId + 1, people[0].Id);
                Assert.AreEqual(maxId + 2, people[1].Id);
                Assert.AreEqual(maxId + 3, people[2].Id);
            }
        }
Exemplo n.º 2
0
        public void Removing_A_Column_Mapping_Works()
        {
            using (_Connection)
            {
                List <Person> people = new List <Person>()
                {
                    new Person()
                    {
                        Id = 101, Name = "InsertTest-1", IsMarried = false, DateOfBirth = new DateTime(1985, 2, 17), PlaceOfBirth = "Zurich",
                    },
                    new Person()
                    {
                        Id = 102, Name = "InsertTest-2", IsMarried = true, DateOfBirth = new DateTime(1972, 11, 2), PlaceOfBirth = "London",
                    },
                    new Person()
                    {
                        Id = 103, Name = "InsertTest-3", IsMarried = false, DateOfBirth = new DateTime(1953, 8, 15), PlaceOfBirth = "New York",
                    },
                };

                InsertCommand <Person> cmd = new InsertCommand <Person>(_Connection, "People");
                cmd.CreateDynamicMappings(new string[] { "Id" });
                cmd.RemoveMapping("PlaceOfBirth");
                cmd.Execute(people);

                // check
                var checkCmd = _Connection.CreateCommand();
                checkCmd.CommandText = @"
SELECT COUNT(*) 
FROM People 
WHERE (Name = 'InsertTest-1' AND IsMarried = 0 AND PlaceOfBirth IS NULL) 
OR (Name = 'InsertTest-2' AND IsMarried = 1 AND PlaceOfBirth IS NULL) 
OR (Name = 'InsertTest-3' AND IsMarried = 0 AND PlaceOfBirth IS NULL)";

                Assert.AreEqual(3, checkCmd.ExecuteScalar());
            }
        }