/// <summary> /// Get the primary key of a given <see cref="Type"/> /// </summary> /// <param name="model"></param> /// <param name="schema"></param> /// <returns></returns> public static PropertyInfo GetPrimaryKey(this DBSchema schema, Type model) { // Get the DBTable containing the constraints for the dbcontext var table = schema.GetType().GetMethod("Model").MakeGenericMethod(model).Invoke(schema, null); // Return the type of the primary key return((PropertyInfo)table.GetType().GetProperty("PrimaryKey").GetValue(table)); }
/// <summary> /// Gets wether the <see cref="PropertyInfo"/> given is the Primary Key in the given Schema /// </summary> /// <param name="propertyInfo">The <see cref="PropertyInfo"/> to check</param> /// <param name="Schema">The Schema to use</param> /// <param name="type">The model for wich we're running the action</param> /// <returns></returns> public static bool IsPrimaryKey(this DBSchema Schema, PropertyInfo propertyInfo, Type model) { var output = Schema.GetType().GetMethod("Model").MakeGenericMethod(model).Invoke(Schema, null); if ((PropertyInfo)output.GetType().GetProperty("PrimaryKey").GetValue(output) == propertyInfo) { return(true); } return(false); }
/// <summary> /// /// </summary> /// <param name="Schema"></param> /// <param name="obj"></param> /// <returns></returns> public static bool RequiredValuesCorrect(this DBSchema Schema, object obj) { bool retval = true; var output = Schema.GetType().GetMethod("Model").MakeGenericMethod(obj.GetType()).Invoke(Schema, null); HashSet <MemberInfo> requiredProps = (HashSet <MemberInfo>)output.GetType().GetProperty("RequiredProperties").GetValue(output); foreach (var prop in obj.GetType().GetProperties()) { if (requiredProps.Contains(prop)) { if ((requiredProps.Where(m => m == prop).First() as PropertyInfo)?.GetValue(obj) == null) { retval = false; } } } return(retval); }