Esempio n. 1
0
        public ModelInfo(Type _type)
        {
            this.MType     = _type;
            this.Fillables = new List <ModelField>();
            this.Table     = _type.GetCustomAttribute <Table>();

            if (Table == null)
            {
                throw new ModelFormatException(string.Format("Missing table attribute for Model {0}", MType.ToString()));
            }

            foreach (FieldInfo field in _type.GetFields())
            {
                if (field.GetCustomAttribute(typeof(Fillable)) != null)
                {
                    ModelField modelField = new ModelField(field);
                    if (PrimaryField == null && modelField.GetPrimaryKey() != null)
                    {
                        this.PrimaryField = modelField;
                    }

                    Fillables.Add(modelField);
                }
            }
        }
Esempio n. 2
0
        public static T Find <T>(int id) where T : Model
        {
            ModelField primary = GetInfo <T>().PrimaryField;

            if (primary == null)
            {
                throw new ModelFormatException("Model does not contain a PrimaryKey Attribute");
            }

            List <T> models = Select <T>().Where(primary.GetFillable().Field, id).Get();

            if (models.Count < 1)
            {
                return(null);
            }

            return(models[0]);
        }