public IHttpActionResult SaveScreen([FromBody] List<KeyValuePair<SaveEntityVm, List<SavePropertyVm>>> saveData)
        {
            var queryBuilder = new FluentQueryBuilder();

            foreach (var saveGroup in saveData)
            {
                var entity = new Entity() { guid = saveGroup.Key.entityGuid };
                var entityInstance = new EntityInstance() { guid = saveGroup.Key.entityInstanceGuid };

                queryBuilder.Merge(entityInstance);
                queryBuilder.RelateTo(entityInstance, new InstanceOf(), entity);

                foreach (var propertyGroup in saveGroup.Value)
                {

                    var property = new Property() { guid = propertyGroup.propertyGuid };

                    if (propertyGroup.type == "dataProperty")
                    {
                        var propertyValue = new PropertyValue() {guid = Guid.NewGuid().ToString("N"), value = propertyGroup.value};
                        queryBuilder.Merge(propertyValue);
                        queryBuilder.Replace(entityInstance, new HasPropertyValue(), new PropertyValue(), entityInstance, new HasPropertyValue(), propertyValue, property);
                        queryBuilder.RelateTo(propertyValue, new IsValueForProperty(), property);
                    }
                    else if (propertyGroup.type == "entityProperty")
                    {
                        var propertyValueInstance = new EntityInstance() { guid = propertyGroup.value };
                        queryBuilder.Replace(entityInstance, new HasPropertyValue(), new EntityInstance(), entityInstance, new HasPropertyValue(), propertyValueInstance, property);
                        queryBuilder.RelateTo(propertyValueInstance, new IsValueForProperty(), property);
                    }

                    
                }

            }

            queryBuilder.Execute(StagedDbContext.Instance.GraphClient.Cypher);

            
            return Ok();
        }
        public IEnumerable<EntityInstanceVm> GetByEntityAndId([FromUri] EntityInstanceFilter filter)
        {

            if(filter.entity == null) throw new ArgumentNullException("filter.entity is null bro");
            if (filter.entityInstance == null) throw new ArgumentNullException("filter.entityInstance is null bro");

            var entity = MetadataCache.Instance.Entities.Select(x => new Entity{ guid = x.guid, name = x.name }).FirstOrDefault(x => x.guid == filter.entity);

            if (entity == null) throw new NullReferenceException(string.Format("no entity with the guid {0} in the metadata", filter.entity));

            var entityInstance = new EntityInstance { guid = filter.entityInstance};


            switch (filter.level)
            {
                case 3:
                    return Level3(entity, entityInstance, 0, 1);
                case 2:
                    return Level2(entity, entityInstance, 0, 1);
                default:
                    return Level1(entity, entityInstance, 0, 1);
            }
        }
        private IEnumerable<EntityInstanceVm> Level3(Entity matchEntity, EntityInstance matchEntityInstance, int skip, int limit)
        {

            var entityMatchNode = matchEntity.MatchNode("entity");
            var entityInstanceMatchNode = matchEntityInstance == null ? new EntityInstance().NodeNameWithAlias("entityInstance") : matchEntityInstance.MatchNode("entityInstance");

            var query = StagedDbContext.Instance.GraphClient.Cypher
                .Match(string.Format("{0}<-[:INSTANCE_OF]-{1}", entityMatchNode, entityInstanceMatchNode))
                .With("entityInstance, entity, count(entityInstance) AS totalCount")
                .Skip(skip)
                .Limit(limit)
                .Match("(property:Property)<-[:HAS_PROPERTY]-(entity)")
                .With("entityInstance, entity, property, totalCount")

                .OptionalMatch("(property)-[:FOR_ENTITY]->(entityPropertyEntity:Entity)")
                .OptionalMatch("(entityInstance)-[:HAS_PROPERTY_VALUE]->(dataPropertyPropertyValue:PropertyValue)-[:IS_VALUE_FOR_PROPERTY]->(property)")
                .OptionalMatch("(entityInstance)-[:HAS_PROPERTY_VALUE]->(entityPropertyEntityInstance:EntityInstance)-[:IS_VALUE_FOR_PROPERTY]->(property)-[:FOR_ENTITY]->(entityPropertyEntity)")

                .OptionalMatch("(property2:Property)<-[:HAS_PROPERTY]-(entityPropertyEntity)<-[:INSTANCE_OF]-(entityPropertyEntityInstance)")

                .OptionalMatch("(property2)-[:FOR_ENTITY]->(entityPropertyEntity2:Entity)")
                .OptionalMatch("(entityPropertyEntityInstance)-[:HAS_PROPERTY_VALUE]->(dataPropertyPropertyValue2:PropertyValue)-[:IS_VALUE_FOR_PROPERTY]->(property2)")
                .OptionalMatch("(entityPropertyEntityInstance)-[:HAS_PROPERTY_VALUE]->(entityPropertyEntityInstance2:EntityInstance)-[:IS_VALUE_FOR_PROPERTY]->(property2)-[:FOR_ENTITY]->(entityPropertyEntity2)")

                .OptionalMatch("(property3:Property)<-[:HAS_PROPERTY]-(entityPropertyEntity2)<-[:INSTANCE_OF]-(entityPropertyEntityInstance2)")

                .OptionalMatch("(property3)-[:FOR_ENTITY]->(entityPropertyEntity3:Entity)")
                .OptionalMatch("(entityPropertyEntityInstance2)-[:HAS_PROPERTY_VALUE]->(dataPropertyPropertyValue3:PropertyValue)-[:IS_VALUE_FOR_PROPERTY]->(property3)")
                .OptionalMatch("(entityPropertyEntityInstance2)-[:HAS_PROPERTY_VALUE]->(entityPropertyEntityInstance3:EntityInstance)-[:IS_VALUE_FOR_PROPERTY]->(property3)-[:FOR_ENTITY]->(entityPropertyEntity3)")


                .Return((entity, entityInstance, 
                property, dataPropertyPropertyValue, entityPropertyEntityInstance, entityPropertyEntity,
                property2, dataPropertyPropertyValue2, entityPropertyEntityInstance2, entityPropertyEntity2,
                property3, dataPropertyPropertyValue3, entityPropertyEntityInstance3, entityPropertyEntity3) => new
                {

                    //first
                    entity = (entity.As<Entity>()),
                    entityInstance = entityInstance.As<EntityInstance>(),
                    entityPropertyEntityInstance = entityPropertyEntityInstance.As<EntityInstance>(),
                    dataPropertyPropertyValue = dataPropertyPropertyValue.As<PropertyValue>(),
                    property = property.As<Property>(),

                    entityPropertyEntity = entityPropertyEntity.As<Entity>(),

                    property2 = property2.As<Property>(),
                    dataPropertyPropertyValue2 = dataPropertyPropertyValue2.As<PropertyValue>(),
                    entityPropertyEntityInstance2 = entityPropertyEntityInstance2.As<EntityInstance>(),

                    entityPropertyEntity2 = entityPropertyEntity2.As<Entity>(),

                    property3 = property3.As<Property>(),
                    dataPropertyPropertyValue3 = dataPropertyPropertyValue3.As<PropertyValue>(),
                    entityPropertyEntityInstance3 = entityPropertyEntityInstance3.As<EntityInstance>(),

                    entityPropertyEntity3 = entityPropertyEntity3.As<Entity>(),
                });



            var results = query.Results
                .GroupBy(x =>
                    new
                    {
                        entityInstanceGuid = x.entityInstance.guid,
                        entityName = x.entity.name,
                        entityGuid = x.entity.guid,
                    }
                )
                .Select(y => new
                    //asset
                    EntityInstanceVm
                {
                    guid = y.Key.entityInstanceGuid,
                    entity = new PartialEntityVm { name = y.Key.entityName, guid = y.Key.entityGuid },
                    dataProperties = y.Where(ww => ww.property.dataType != DataTypeEnum.EntityInstance)
                    .GroupBy(az => new
                    {
                        propertyGuid = az.property.guid,
                        propertyName = az.property.name,
                    })
                    .Select(bz => new DataPropertyInstanceVm
                    {
                        guid = bz.Key.propertyGuid,
                        name = bz.Key.propertyName,
                        value = bz.First().dataPropertyPropertyValue == null ? null : bz.First().dataPropertyPropertyValue.value
                    }),
                    entityProperties = y.Where(www => www.property.dataType == DataTypeEnum.EntityInstance)
                    .GroupBy(z =>
                        new
                        {
                            propertyGuid = z.property.guid,
                            propertyName = z.property.name,
                            entityPropertyEntityName = z.entityPropertyEntity.name,
                            entityPropertyEntityGuid = z.entityPropertyEntity.guid,
                        }
                    )
                    .Select(entityInstanceProperties =>
                        new EntityPropertyInstanceVm
                        {
                            guid = entityInstanceProperties.Key.propertyGuid,
                            name = entityInstanceProperties.Key.propertyName,
                            //asset type
                            entity = new PartialEntityVm { guid = entityInstanceProperties.Key.entityPropertyEntityGuid, name = entityInstanceProperties.Key.entityPropertyEntityName },
                            entityInstanceGuid = entityInstanceProperties.First().entityPropertyEntityInstance == null ? null : entityInstanceProperties.First().entityPropertyEntityInstance.guid,
                            entityInstance = entityInstanceProperties.First().entityPropertyEntityInstance == null ? null : new EntityInstanceVm
                            {
                                guid = entityInstanceProperties.First().entityPropertyEntityInstance == null ? null : entityInstanceProperties.First().entityPropertyEntityInstance.guid,
                                entity = new PartialEntityVm { guid = entityInstanceProperties.Key.entityPropertyEntityGuid, name = entityInstanceProperties.Key.entityPropertyEntityName },
                                dataProperties = entityInstanceProperties.Where(ww => ww.property2 != null && ww.property2.dataType != DataTypeEnum.EntityInstance)
                                .GroupBy(az => new
                                {
                                    propertyGuid = az.property2.guid,
                                    propertyName = az.property2.name,
                                })
                                .Select(oz => new DataPropertyInstanceVm
                                {
                                    guid = oz.Key.propertyGuid,
                                    name = oz.Key.propertyName,
                                    value = oz.First().dataPropertyPropertyValue2 == null ? null : oz.First().dataPropertyPropertyValue2.value
                                }),
                                entityProperties = entityInstanceProperties.Where(ww => ww.property2 != null && ww.property2.dataType == DataTypeEnum.EntityInstance)
                                .GroupBy(pz => new
                                {
                                    propertyGuid = pz.property2.guid,
                                    propertyName = pz.property2.name,
                                    entityPropertyEntityName = pz.entityPropertyEntity2.name,
                                    entityPropertyEntityGuid = pz.entityPropertyEntity2.guid,
                                })
                                .Select(entityInstanceProperties2 => new EntityPropertyInstanceVm
                                {
                                    guid = entityInstanceProperties2.Key.propertyGuid,
                                    name = entityInstanceProperties2.Key.propertyName,
                                    entity = new PartialEntityVm { guid = entityInstanceProperties2.Key.entityPropertyEntityGuid, name = entityInstanceProperties2.Key.entityPropertyEntityName },
                                    entityInstanceGuid = entityInstanceProperties2.First().entityPropertyEntityInstance2 == null ? null : entityInstanceProperties2.First().entityPropertyEntityInstance2.guid,
                                    entityInstance = entityInstanceProperties2.First().entityPropertyEntityInstance == null ? null : new EntityInstanceVm
                                    {
                                        guid = entityInstanceProperties2.First().entityPropertyEntityInstance == null ? null : entityInstanceProperties2.First().entityPropertyEntityInstance.guid,
                                        entity = new PartialEntityVm { guid = entityInstanceProperties2.Key.entityPropertyEntityGuid, name = entityInstanceProperties2.Key.entityPropertyEntityName },
                                        dataProperties = entityInstanceProperties2.Where(ww => ww.property3 != null && ww.property3.dataType != DataTypeEnum.EntityInstance)
                                        .GroupBy(az => new
                                        {
                                            propertyGuid = az.property3.guid,
                                            propertyName = az.property3.name,
                                        })
                                        .Select(oz => new DataPropertyInstanceVm
                                        {
                                            guid = oz.Key.propertyGuid,
                                            name = oz.Key.propertyName,
                                            value = oz.First().dataPropertyPropertyValue3 == null ? null : oz.First().dataPropertyPropertyValue3.value
                                        }),
                                        entityProperties = entityInstanceProperties2.Where(ww => ww.property3 != null && ww.property3.dataType == DataTypeEnum.EntityInstance)
                                        .GroupBy(pz => new
                                        {
                                            propertyGuid = pz.property3.guid,
                                            propertyName = pz.property3.name,
                                            entityPropertyEntityName = pz.entityPropertyEntity3.name,
                                            entityPropertyEntityGuid = pz.entityPropertyEntity3.guid,
                                        })
                                        .Select(pp => new EntityPropertyInstanceVm
                                        {
                                            guid = pp.Key.propertyGuid,
                                            name = pp.Key.propertyName,
                                            entity = new PartialEntityVm { guid = pp.Key.entityPropertyEntityGuid, name = pp.Key.entityPropertyEntityName },
                                            entityInstanceGuid = pp.First().entityPropertyEntityInstance3 == null ? null : pp.First().entityPropertyEntityInstance3.guid,
                                        }),

                                    }
                                }),

                            }
                        }
                    )

                }).ToList();

            var all = results.SelectMany(m => m.entityProperties, (parent, child) => child.entityInstance)
                .Union(results.Select(p => new EntityInstanceVm
                {
                    guid = p.guid,
                    entity = p.entity,
                    entityProperties = p.entityProperties,
                    dataProperties = p.dataProperties

                }))
                .Where(g => g != null)
                .GroupBy(by => by.guid)
                .Select(group => group.First())
                .Union(
                    results.SelectMany(m => m.entityProperties, (parent, child) => child.entityInstance)
                    .Where(g => g != null)
                    .SelectMany(m => m.entityProperties, (parent, child) => child.entityInstance)
                    .GroupBy(by => by.guid)
                    .Select(group => group.First())
                )
                ;

            return all;
        }
        private IEnumerable<EntityInstanceVm> Level1(Entity matchEntity, EntityInstance matchEntityInstance, int skip, int limit)
        {

            var entityMatchNode = matchEntity.MatchNode("entity");
            var entityInstanceMatchNode = matchEntityInstance == null ? new EntityInstance().NodeNameWithAlias("entityInstance") : matchEntityInstance.MatchNode("entityInstance");

            var query = StagedDbContext.Instance.GraphClient.Cypher
                .Match(string.Format("{0}<-[:INSTANCE_OF]-{1}", entityMatchNode, entityInstanceMatchNode))
                .With("entityInstance, entity, count(entityInstance) AS totalCount")
                .Skip(skip)
                .Limit(limit)
                .Match("(property:Property)<-[:HAS_PROPERTY]-(entity)")
                .With("entityInstance, entity, property, totalCount")
                .OptionalMatch("(property)-[:FOR_ENTITY]->(entityPropertyEntity:Entity)")
                .OptionalMatch("(entityInstance)-[:HAS_PROPERTY_VALUE]->(dataPropertyPropertyValue:PropertyValue)-[:IS_VALUE_FOR_PROPERTY]->(property)")
                .OptionalMatch("(entityInstance)-[:HAS_PROPERTY_VALUE]->(entityPropertyEntityInstance:EntityInstance)-[:IS_VALUE_FOR_PROPERTY]->(property)-[:FOR_ENTITY]->(entityPropertyEntity)")
                .Return((entity, entityInstance, property, entityPropertyEntity, dataPropertyPropertyValue, entityPropertyEntityInstance, totalCount) => new
                {

                    //for each entity asset
                    entity = (entity.As<Entity>()),
                    entityInstance = entityInstance.As<EntityInstance>(),

                    //for each property asset type
                    entityPropertyEntityInstance = entityPropertyEntityInstance.As<EntityInstance>(),
                    entityPropertyEntity = entityPropertyEntity.As<Entity>(),
                    dataPropertyPropertyValue = dataPropertyPropertyValue.As<PropertyValue>(),
                    property = property.As<Property>(),
                    totalCount = totalCount.As<int>()
                });



            var results = query.Results
                .GroupBy(x =>
                    new
                    {
                        entityInstanceGuid = x.entityInstance.guid,
                        entityName = x.entity.name,
                        entityGuid = x.entity.guid,
                    }
                )
                .Select(y => new
                    //asset
                    EntityInstanceVm
                {
                    guid = y.Key.entityInstanceGuid,
                    entity = new PartialEntityVm { name = y.Key.entityName, guid = y.Key.entityGuid },
                    dataProperties = y.GroupBy(az => new { 
                        propertyValue = az.dataPropertyPropertyValue,
                        dataProperty = az.property
                    })
                    .Where(ww => ww.Key.dataProperty.dataType != DataTypeEnum.EntityInstance)
                    .Select(bz => new DataPropertyInstanceVm { 
                        guid = bz.Key.dataProperty.guid,
                        name = bz.Key.dataProperty.name,
                        value = bz.Key.propertyValue == null ? null : bz.Key.propertyValue.value 
                    }),
                    entityProperties = y
                    .GroupBy(z =>
                        new
                        {
                            entityProperty = z.property,
                            entityInstance = z.entityPropertyEntityInstance,
                            entityInstanceEntity = z.entityPropertyEntity,

                        }
                    )
                    .Where(www => www.Key.entityProperty.dataType == DataTypeEnum.EntityInstance)
                    .Select(entityInstanceProperties =>
                        new EntityPropertyInstanceVm
                        {
                            guid = entityInstanceProperties.Key.entityProperty.guid,
                            name = entityInstanceProperties.Key.entityProperty.name,
                            //asset type
                            entity = new PartialEntityVm { guid = entityInstanceProperties.Key.entityInstanceEntity.guid, name = entityInstanceProperties.Key.entityInstanceEntity.name },
                            entityInstanceGuid = entityInstanceProperties.Key.entityInstance == null ? null : entityInstanceProperties.Key.entityInstance.guid
                           
                        }
                    )

                });

            return results;
        }
        private void AddEntities(FluentQueryBuilder queryBuilder)
        {






            queryBuilder
             .Create(_asset)
             .Create(_assetType)
             .Create(_assetGroup)
             .Create(_manufacturer)
             .Create(_assetWidthProperty)
             .Create(_assetNameProperty)
             .Create(_assetTypeNameProperty)
             .Create(_assetTypeDescriptionProperty)
             .Create(_assetGroupNameProperty)
             .Create(_manufacturerNameProperty)
             .Create(_asset_parentAssetProperty)

             .Create(_asset_assetTypeProperty)
             .Create(_asset_assetTypeProperty2)
             .Create(_assetType_assetGroupProperty)
             .Create(_assetGroup_manufacturerProperty)
             .Create(_manufacturer_assetGroupProperty)
             .Create(_asset_manufacturerProperty)


             .RelateTo(_asset, new HasProperty { }, _asset_parentAssetProperty)
             .RelateTo(_asset, new HasProperty { }, _asset_assetTypeProperty)
             .RelateTo(_asset, new HasProperty { }, _asset_assetTypeProperty2)
             .RelateTo(_assetType, new HasProperty { }, _assetType_assetGroupProperty)
             .RelateTo(_assetGroup, new HasProperty { }, _assetGroup_manufacturerProperty)
             .RelateTo(_manufacturer, new HasProperty { }, _manufacturer_assetGroupProperty)
             .RelateTo(_asset, new HasProperty { }, _asset_manufacturerProperty)

             .RelateTo(_asset, new HasProperty { }, _assetWidthProperty)
             .RelateTo(_asset, new HasProperty { isDefaultDisplayProperty = true }, _assetNameProperty)
             .RelateTo(_assetType, new HasProperty { isDefaultDisplayProperty = true }, _assetTypeNameProperty)
             .RelateTo(_assetType, new HasProperty { }, _assetTypeDescriptionProperty)

             .RelateTo(_assetGroup, new HasProperty { isDefaultDisplayProperty = true }, _assetGroupNameProperty)
             .RelateTo(_manufacturer, new HasProperty { isDefaultDisplayProperty = true }, _manufacturerNameProperty)


             .RelateTo(_asset_parentAssetProperty, new ForEntity(), _asset)
             .RelateTo(_asset_assetTypeProperty, new ForEntity(), _assetType)
             .RelateTo(_asset_assetTypeProperty2, new ForEntity(), _assetType)
             .RelateTo(_assetType_assetGroupProperty, new ForEntity(), _assetGroup)
             .RelateTo(_assetGroup_manufacturerProperty, new ForEntity(), _manufacturer)
             .RelateTo(_manufacturer_assetGroupProperty, new ForEntity(), _assetGroup)
             .RelateTo(_asset_manufacturerProperty, new ForEntity(), _manufacturer)


             .RelateTo(_seGridWidget, new DisplaysValueFor(), _asset_parentAssetProperty)
             .RelateTo(_dpTextWidget3, new DisplaysValueFor(), _assetTypeDescriptionProperty)
             .RelateTo(_dpTextWidget3, new ForThisHeaderProperty(), _asset_assetTypeProperty)
             .RelateTo(_dpTextWidget, new DisplaysValueFor(), _assetTypeNameProperty)
             .RelateTo(_dpTextWidget, new ForThisHeaderProperty(), _asset_assetTypeProperty)
             .RelateTo(_epDropdownWidget, new DisplaysValueFor(), _assetTypeNameProperty)
             .RelateTo(_epDropdownWidget, new ForThisHeaderProperty(), _asset_assetTypeProperty)

             .RelateTo(_dpTextWidget2, new DisplaysValueFor(), _assetNameProperty)
                //.RelateTo(_dpTextWidget2, new DisplaysValueFor(), _assetWidthProperty)


             //.RelateTo(_eGridTileWidget, new DisplaysValueFor(), _assetTypeNameProperty)
             .RelateTo(_eGridTileWidget, new DisplaysValueFor(), _assetNameProperty)
             .RelateTo(_seGridTileWidget, new DisplaysValueFor(), _assetNameProperty)
             ;


            EntityInstance previousAssetInstance = null;

            for (int i = 0; i < 5; i++)
            {
                string assetInstanceGuid = Guid.NewGuid().ToString("N");
                if (i == 0) assetInstanceGuid = "cd49c069209b40aea12b63ae71bfd324";

                var assetInstance = new EntityInstance() { guid = assetInstanceGuid };
                var assetTypeInstance = new EntityInstance() { guid = Guid.NewGuid().ToString("N") };
                var manufacturerInstance = new EntityInstance() { guid = Guid.NewGuid().ToString("N") };
                var assetGroupInstance = new EntityInstance() { guid = Guid.NewGuid().ToString("N") };
                var propertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "100" + i };
                var assetNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset name" + i };
                var assetTypeNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset type name" + i };
                var assetTypeDescriptionPropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset type Description" + i };


                var assetGroupNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Asset group name" + i };
                var manufacturerNamePropertyValue = new PropertyValue() { guid = Guid.NewGuid().ToString("N"), value = "Manufacturer name" + i };

                queryBuilder
                .Create(assetInstance)
                .Create(assetTypeInstance)
                .Create(assetGroupInstance)
                .Create(manufacturerInstance)
                .Create(propertyValue)
                .Create(assetNamePropertyValue)
                .Create(assetTypeNamePropertyValue)
                .Create(assetTypeDescriptionPropertyValue)
                .Create(assetGroupNamePropertyValue)
                .Create(manufacturerNamePropertyValue)

                .RelateTo(assetInstance, new HasPropertyValue(), assetNamePropertyValue)
                .RelateTo(assetTypeInstance, new HasPropertyValue(), assetTypeNamePropertyValue)
                .RelateTo(assetTypeInstance, new HasPropertyValue(), assetTypeDescriptionPropertyValue)
                
                .RelateTo(assetGroupInstance, new HasPropertyValue(), assetGroupNamePropertyValue)
                .RelateTo(manufacturerInstance, new HasPropertyValue(), manufacturerNamePropertyValue)
                .RelateTo(assetInstance, new HasPropertyValue(), propertyValue)

                .RelateTo(assetInstance, new HasPropertyValue(), assetTypeInstance)
                .RelateTo(assetTypeInstance, new HasPropertyValue(), assetGroupInstance)
                .RelateTo(assetGroupInstance, new HasPropertyValue(), manufacturerInstance)
                .RelateTo(manufacturerInstance, new HasPropertyValue(), assetGroupInstance)
                .RelateTo(assetInstance, new HasPropertyValue(), manufacturerInstance)
                

                .RelateTo(assetTypeInstance, new IsValueForProperty(), _asset_assetTypeProperty)
                .RelateTo(assetTypeInstance, new IsValueForProperty(), _asset_assetTypeProperty2)
                .RelateTo(assetGroupInstance, new IsValueForProperty(), _assetType_assetGroupProperty)

                .RelateTo(manufacturerInstance, new IsValueForProperty(), _assetGroup_manufacturerProperty)
                .RelateTo(assetGroupInstance, new IsValueForProperty(), _manufacturer_assetGroupProperty)


                .RelateTo(propertyValue, new IsValueForProperty(), _assetWidthProperty)

                .RelateTo(assetNamePropertyValue, new IsValueForProperty(), _assetNameProperty)
                .RelateTo(assetTypeNamePropertyValue, new IsValueForProperty(), _assetTypeNameProperty)
                .RelateTo(assetTypeDescriptionPropertyValue, new IsValueForProperty(), _assetTypeDescriptionProperty)
                
                .RelateTo(assetGroupNamePropertyValue, new IsValueForProperty(), _assetGroupNameProperty)
                .RelateTo(manufacturerNamePropertyValue, new IsValueForProperty(), _manufacturerNameProperty)


                .RelateTo(assetInstance, new InstanceOf(), _asset)
                .RelateTo(assetTypeInstance, new InstanceOf(), _assetType)
                .RelateTo(assetGroupInstance, new InstanceOf(), _assetGroup)
                .RelateTo(manufacturerInstance, new InstanceOf(), _manufacturer)
                ;


                if (previousAssetInstance != null)
                    queryBuilder
                        .RelateTo(previousAssetInstance, new HasPropertyValue(), assetInstance)
                        .RelateTo(previousAssetInstance, new IsValueForProperty(), _asset_parentAssetProperty);

                previousAssetInstance = assetInstance;
            }
        }