private void InitializeDbSetMembers()
        {
            const BindingFlags constructorBindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
            const BindingFlags memberBindingFlags      = BindingFlags.Public | BindingFlags.Instance;

            var contextType = GetType();

            var dbSetFields = contextType
                              .GetFields(memberBindingFlags)
                              .Where(
                f => f.FieldType.IsGenericType &&
                f.FieldType
                .GetInterfaces()
                .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDbSet <>))
                );


            foreach (var dbSetField in dbSetFields)
            {
                var instanceType = dbSetField.FieldType;
                EntityValidator.ValidateDefinitionAndThrow(instanceType.GetGenericArguments().First());
                var instance = Activator.CreateInstance(instanceType, constructorBindingFlags, null, new object[] { this }, null);

                dbSetField.SetValue(this, instance);
            }

            var dbSetProperties = contextType
                                  .GetProperties(memberBindingFlags)
                                  .Where(
                f => f.PropertyType.IsGenericType &&
                f.PropertyType
                .GetInterfaces()
                .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDbSet <>))
                );

            foreach (var dbSetProperty in dbSetProperties)
            {
                var instanceType = dbSetProperty.PropertyType;
                EntityValidator.ValidateDefinitionAndThrow(instanceType.GetGenericArguments().First());
                var instance = Activator.CreateInstance(instanceType, constructorBindingFlags, null, new object[] { this }, null);

                dbSetProperty.SetValue(this, instance, null);
            }
        }
 public void TestInternalClass_DoesNotThrow()
 {
     Assert.DoesNotThrow(() => EntityValidator.ValidateDefinitionAndThrow(typeof(Data.TestA)));
 }
 public void TestInternalClass_Throws()
 {
     Assert.Throws <EntityValidationException>(() => EntityValidator.ValidateDefinitionAndThrow(typeof(TestE)));
 }
 public void TestSingleIdentityKey_Throws()
 {
     Assert.Throws <EntityValidationException>(() => EntityValidator.ValidateDefinitionAndThrow(typeof(TestD)));
 }
 public void TestIdentityAndCustomKeys_Throws()
 {
     Assert.Throws <EntityValidationException>(() => EntityValidator.ValidateDefinitionAndThrow(typeof(TestC)));
 }
 public void TestIdentityColumnTypes_Throws()
 {
     Assert.Throws <EntityValidationException>(() => EntityValidator.ValidateDefinitionAndThrow(typeof(TestB)));
 }
 public void TestWithoutKeys_Throws()
 {
     Assert.Throws <EntityValidationException>(() => EntityValidator.ValidateDefinitionAndThrow(typeof(TestA)));
 }