protected ActiveRecordBase SetDetailsKeys(ActiveRecordBase entityMaster, ActiveRecordBase entityDetail, KPFormItemKeyFieldsCollection keyFieldsConfig)
        {
            if (keyFieldsConfig != null)
            {
                foreach (KPMappingKeyModel key in keyFieldsConfig)
                {
                    PropertyInfo propMaster = entityMaster.GetType().GetProperty(key.KeyMaster);
                    PropertyInfo propDetail = entityDetail.GetType().GetProperty(key.KeyDetail);
                    if (propMaster != null && propDetail != null)
                    {
                        object value = propMaster.GetValue(entityMaster, null);

                        if (value == null)
                            continue;

                        int idKey = 0;
                        if (Int32.TryParse(value.ToString(), out idKey))
                        {
                            // Proteção para não trazer nada quando novo
                            if (idKey == 0)
                                continue;
                        }

                        if (propMaster.PropertyType == propDetail.PropertyType)
                        {
                            propDetail.SetValue(entityDetail, value, null);
                        }
                        else
                        {
                            if (typeof(ActiveRecordBase).IsSubclassOfRawGeneric(propDetail.PropertyType))
                            {
                                MethodInfo methodFind = propDetail.PropertyType.GetMethodInheritance("Find", new Type[] { typeof(object) });
                                if (methodFind != null)
                                {
                                    object objValue = methodFind.Invoke(null, new object[] { value });
                                    if (objValue != null)
                                    {
                                        propDetail.SetValue(entityDetail, objValue, null);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return entityDetail;
        }
        private ICriterion GetFiltering(KPFormItemKeyFieldsCollection keys, ActiveRecordBase entity)
        {
            Conjunction conjuntion = new Conjunction();

            if (entity != null && MasterDetailConfig.TypeEntityDetail != null)
            {
                foreach (KPMappingKeyModel key in keys)
                {
                    PropertyInfo propMaster = entity.GetType().GetProperty(key.KeyMaster);
                    PropertyInfo propDetail = MasterDetailConfig.TypeEntityDetail.GetProperty(key.KeyDetail);
                    if (propMaster != null && propDetail != null)
                    {
                        object value = propMaster.GetValue(entity, null);

                        int idKey = 0;
                        if (Int32.TryParse(value.ToString(), out idKey))
                        {
                            // Proteção para não trazer nada quando novo
                            if (idKey == 0)
                            {
                                conjuntion.Add(Expression.Sql(" 1 = 2"));
                                return conjuntion;
                            }

                        }

                        if (propMaster.PropertyType == propDetail.PropertyType)
                        {
                            conjuntion.Add(Expression.Eq(key.KeyDetail, value));
                        }
                        else
                        {
                            if (typeof(ActiveRecordBase).IsSubclassOfRawGeneric(propDetail.PropertyType))
                            {
                                MethodInfo methodFind = propDetail.PropertyType.GetMethodInheritance("Find", new Type[] { typeof(object) });
                                if (methodFind != null)
                                {
                                    object objValue = methodFind.Invoke(null, new object[] { value });
                                    if (objValue != null)
                                    {
                                        conjuntion.Add(Expression.Eq(key.KeyDetail, objValue));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return conjuntion;
        }