public async Task TestInsertWithReference()
        {
            var testPerson = IntegrationTestEnvironment.TestData <BaPersonTestData>().Vasquez;

            // Arrange
            const string strasse    = "Teststrasse";
            var          baPersonId = testPerson.BaPersonID;

            var adresse = new BaAdresse
            {
                BaPerson   = testPerson,
                BaPersonId = testPerson.BaPersonID,
                Strasse    = strasse
            };

            // Act
            using (new EnsureExecutionScope(IntegrationTestEnvironment.Container))
            {
                var context  = IntegrationTestEnvironment.Container.GetInstance <IDbContext>();
                var adressen = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaAdresse> >();

                await adressen.InsertOrUpdateEntity(adresse);

                await context.SaveChangesAsync();
            }
            var baPersonIdInserted = adresse.BaPersonId;

            // Assert
            adresse.BaAdresseId.ShouldNotBe(0);
            baPersonIdInserted.ShouldBe(baPersonId, "Referenzierte Person darf nicht noch einmal eingefügt werden");
            BaAdresse adresseAssert;

            using (new EnsureExecutionScope(IntegrationTestEnvironment.Container))
            {
                var adressen = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaAdresse> >();
                adresseAssert = await adressen.GetById(adresse.BaAdresseId);
            }
            adresseAssert.Strasse.ShouldBe(strasse);
            adresseAssert.BaPersonId.ShouldBe(baPersonId);
        }
        public async Task TestImplicitTransactionDuringInsertOfIndependentEntities()
        {
            // Arrange
            var person = new BaPerson
            {
                Name    = "Meier",
                Vorname = "Hans"
            };
            var adresse = new BaAdresse
            {
                BaPersonId = -1, //invalid
                DatumVon   = DateTime.Today,
            };

            // Act
            using (new EnsureExecutionScope(IntegrationTestEnvironment.Container))
            {
                var context  = IntegrationTestEnvironment.Container.GetInstance <IDbContext>();
                var adressen = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaAdresse> >();
                var personen = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaPerson> >();

                await adressen.InsertOrUpdateEntity(adresse);

                await personen.InsertOrUpdateEntity(person);

                var ex = await Should.ThrowAsync <DbUpdateException>(context.SaveChangesAsync(), "Die Adresse dürfte wegen der ungültigen BaPersonID nicht eingefügt werden");

                var sqlEx = ex.InnerException.ShouldBeOfType <SqlException>();
                sqlEx.Number.ShouldBe(547);
            }

            // Assert
            using (new EnsureExecutionScope(IntegrationTestEnvironment.Container))
            {
                var personen     = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaPerson> >();
                var personAssert = await personen.GetById(person.BaPersonID);

                personAssert.ShouldBeNull("Durch das implizite Transaktionshandling des DbContext dürfte das Insert der Person nicht commited werden");
            }
        }
        public async Task Test_DontUpdateUnchangedReferencedEntity()
        {
            var testUser    = IntegrationTestEnvironment.TestData <XUserTestData>().Administrator;
            var testAdresse = new BaAdresse
            {
                UserId    = testUser.UserID,
                Bemerkung = "testadresse"
            };

            using (new EnsureExecutionScope(IntegrationTestEnvironment.Container))
            {
                var context  = IntegrationTestEnvironment.Container.GetInstance <Kiss4DbContext>();
                var adressen = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaAdresse> >();

                await adressen.InsertOrUpdateEntity(testAdresse);

                await context.SaveChangesAsync();
            }

            // Arrange
            using (new EnsureExecutionScope(IntegrationTestEnvironment.Container))
            {
                var context  = IntegrationTestEnvironment.Container.GetInstance <Kiss4DbContext>();
                var adressen = IntegrationTestEnvironment.Container.GetInstance <IRepository <BaAdresse> >();
                var adresse  = await adressen.Where(adr => adr.Id == testAdresse.BaAdresseId)
                               .Include(adr => adr.User)
                               .FirstOrDefaultAsync();

                // Act
                adresse.Bemerkung += "blabla";
                await adressen.InsertOrUpdateEntity(adresse);

                // wird XUser updated? falls ja, gibt es eine exception, weil keine HistoryVersion erstellt wurde
                await context.SaveChangesAsync();

                // Assert
                // nop, keine exception heisst ok
            }
        }