public void SuperEntityRef_PersistAndLoadWithSingleTypeA_RetrievedShouldBeSameAsPersisted()
        {
            try
            {
                var          con         = SetupTables();
                ITransaction transaction = CreateTransaction(con);

                int id = 35;
                SuperEntityRefRootEntity entity = CreateDefaultRootEntity(id, 1, 0, true, false);
                entity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(con);
                SuperEntityRefRootEntity entityReloaded = new SuperEntityRefRootEntity();
                LoadEntityWithId(transaction, entityReloaded, id);
                con.Close();

                VerifyEquals(entity, entityReloaded);
            }
            catch (System.Exception e)
            {
                LogManager.GetLogger(typeof(DbGateSuperEntityRefTest)).Fatal(e.Message, e);
                Assert.Fail(e.Message);
            }
        }
        private SuperEntityRefRootEntity CreateDefaultRootEntity(int id, int typeACount, int typeBCount, bool one2OneIsA, bool one2OneIsB)
        {
            string entityText = string.Format("Id->{0}|A->{1}|B->{2}|OOA->{3}|OOB->{4}", id, typeACount, typeBCount, one2OneIsA, one2OneIsB);

            SuperEntityRefRootEntity rootEntity = new SuperEntityRefRootEntity();

            rootEntity.IdCol = id;
            rootEntity.Name  = "Root-(" + entityText + ")";

            for (int i = 0; i < typeACount; i++)
            {
                rootEntity.One2ManyEntities.Add(CreateOne2Many(true, entityText, i));
            }
            for (int i = typeACount; i < typeACount + typeBCount; i++)
            {
                rootEntity.One2ManyEntities.Add(CreateOne2Many(false, entityText, i));
            }
            if (one2OneIsA)
            {
                rootEntity.One2OneEntity = CreateOne2One(true, entityText);
            }
            if (one2OneIsB)
            {
                rootEntity.One2OneEntity = CreateOne2One(false, entityText);
            }

            return(rootEntity);
        }
        private bool LoadEntityWithId(ITransaction transaction, SuperEntityRefRootEntity loadEntity, int id)
        {
            bool loaded = false;

            IDbCommand cmd = transaction.CreateCommand();

            cmd.CommandText = "select * from super_entity_ref_test_root where id_col = ?";

            IDbDataParameter parameter = cmd.CreateParameter();

            cmd.Parameters.Add(parameter);
            parameter.DbType    = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value     = id;

            IDataReader dataReader = cmd.ExecuteReader();

            if (dataReader.Read())
            {
                loadEntity.Retrieve(dataReader, transaction);
                loaded = true;
            }

            return(loaded);
        }
        private void VerifyEquals(SuperEntityRefRootEntity rootEntity, SuperEntityRefRootEntity loadedRootEntity)
        {
            Assert.AreEqual(loadedRootEntity.IdCol, rootEntity.IdCol);
            Assert.AreEqual(loadedRootEntity.Name, rootEntity.Name);

            foreach (SuperEntityRefOne2ManyEntity one2ManyEntity in rootEntity.One2ManyEntities)
            {
                bool foundItem = false;
                foreach (SuperEntityRefOne2ManyEntity loadedOne2ManyEntity in loadedRootEntity.One2ManyEntities)
                {
                    if (one2ManyEntity.IndexNo == loadedOne2ManyEntity.IndexNo)
                    {
                        foundItem = true;
                        Assert.AreEqual(one2ManyEntity.GetType(), loadedOne2ManyEntity.GetType());
                        Assert.AreEqual(one2ManyEntity.Name, loadedOne2ManyEntity.Name);

                        if (one2ManyEntity.GetType() == typeof(SuperEntityRefOne2ManyEntityA))
                        {
                            SuperEntityRefOne2ManyEntityA one2ManyEntityA       = (SuperEntityRefOne2ManyEntityA)one2ManyEntity;
                            SuperEntityRefOne2ManyEntityA loadedOne2ManyEntityA = (SuperEntityRefOne2ManyEntityA)loadedOne2ManyEntity;
                            Assert.AreEqual(one2ManyEntityA.NameA, loadedOne2ManyEntityA.NameA);
                        }
                        else if (one2ManyEntity.GetType() == typeof(SuperEntityRefOne2ManyEntityB))
                        {
                            SuperEntityRefOne2ManyEntityB one2ManyEntityA       = (SuperEntityRefOne2ManyEntityB)one2ManyEntity;
                            SuperEntityRefOne2ManyEntityB loadedOne2ManyEntityA = (SuperEntityRefOne2ManyEntityB)loadedOne2ManyEntity;
                            Assert.AreEqual(one2ManyEntityA.NameB, loadedOne2ManyEntityA.NameB);
                        }
                    }
                }
                Assert.IsTrue(foundItem, "Item rootEntity not found");
            }

            SuperEntityRefOne2OneEntity one2OneEntity       = rootEntity.One2OneEntity;
            SuperEntityRefOne2OneEntity loadedOne2OneEntity = loadedRootEntity.One2OneEntity;

            if (one2OneEntity == null || loadedOne2OneEntity == null)
            {
                Assert.IsTrue(one2OneEntity == loadedOne2OneEntity, "One entity is null while other is not");
            }
            else
            {
                Assert.AreEqual(one2OneEntity.GetType(), loadedOne2OneEntity.GetType());
                Assert.AreEqual(one2OneEntity.Name, loadedOne2OneEntity.Name);

                loadedOne2OneEntity = loadedRootEntity.One2OneEntity; //in case of lazy loading
                if (one2OneEntity.GetType() == typeof(SuperEntityRefOne2OneEntityA))
                {
                    SuperEntityRefOne2OneEntityA one2OneEntityA       = (SuperEntityRefOne2OneEntityA)one2OneEntity;
                    SuperEntityRefOne2OneEntityA loadedOne2OneEntityA = (SuperEntityRefOne2OneEntityA)loadedOne2OneEntity;
                    Assert.AreEqual(one2OneEntityA.NameA, loadedOne2OneEntityA.NameA);
                }
                else if (one2OneEntity.GetType() == typeof(SuperEntityRefOne2OneEntityB))
                {
                    SuperEntityRefOne2OneEntityB one2OneEntityB       = (SuperEntityRefOne2OneEntityB)one2OneEntity;
                    SuperEntityRefOne2OneEntityB loadedOne2OneEntityB = (SuperEntityRefOne2OneEntityB)loadedOne2OneEntity;
                    Assert.AreEqual(one2OneEntityB.NameB, loadedOne2OneEntityB.NameB);
                }
            }
        }