public static void BuildCurrentPocos() { var tbls = new Tables(); var iobj = new MyApp.Models.IdentityObject(); var asm = Assembly.GetAssembly(iobj.GetType()); Type[] types = asm.GetTypes(); foreach (Type t in types) { var tbl = new Table(); tbl.Columns = new List <Column>(); Attribute[] attrs = Attribute.GetCustomAttributes(t); foreach (Attribute atr in attrs) { if (atr is NPoco.PrimaryKeyAttribute) { var pka = (NPoco.PrimaryKeyAttribute)atr; tbl.PrimaryKeyNamesFromAttribute = pka.Value; tbl.IsAutoIncrement = tbl.IsAutoIncrement || pka.AutoIncrement; } if (atr is NPoco.TableNameAttribute) { var a = (NPoco.TableNameAttribute)atr; tbl.ClassName = t.Name; tbl.CleanName = a.Value; var props = t.GetProperties(); foreach (var p in props) { var col = new Column(); Attribute[] propAttributes = Attribute.GetCustomAttributes(p); foreach (Attribute patr in propAttributes) { if (patr is StringLengthAttribute) { col.StringLength = ((StringLengthAttribute)patr).MaximumLength; } if (patr is NPoco.ColumnAttribute) { var ca = patr as NPoco.ColumnAttribute; col.Name = String.IsNullOrEmpty(ca.Name) ? p.Name : ca.Name; col.PropertyName = p.Name; col.PropertyType = TypeMap.CommonSystemTypeFromFrameworkType(p.PropertyType.ToString()); col.IsNullable = col.PropertyType.EndsWith("?"); col.IsAutoIncrement = col.IsPK && tbl.IsAutoIncrement; } if (patr is EquivalentTypeForTestingAttribute) { var ca = patr as EquivalentTypeForTestingAttribute; col.PropertyType = ca.Name; col.IsNullable = col.PropertyType.EndsWith("?"); } } if (!String.IsNullOrEmpty(col.PropertyName)) { tbl.Columns.Add(col); } } } } if (!String.IsNullOrEmpty(tbl.ClassName)) { // Assign Primary Key(s) if found if (!String.IsNullOrEmpty(tbl.PrimaryKeyNamesFromAttribute)) { string[] pkNames = tbl.PrimaryKeyNamesFromAttribute.Split(',').Select(a => a.Trim()).ToArray(); var cols = tbl.Columns.Where(a => pkNames.Contains(a.PropertyName)); foreach (var col in cols) { col.IsPK = true; col.IsAutoIncrement = tbl.IsAutoIncrement; } } tbls.Add(tbl); } } currentPocos = tbls; }
public static void BuildCurrentPocos() { var tbls = new Tables(); var iobj = new MyApp.Models.IdentityObject(); var asm = Assembly.GetAssembly(iobj.GetType()); Type[] types = asm.GetTypes(); foreach (Type t in types) { var tbl = new Table(); tbl.Columns = new List<Column>(); Attribute[] attrs = Attribute.GetCustomAttributes(t); foreach (Attribute atr in attrs) { if (atr is NPoco.PrimaryKeyAttribute) { var pka = (NPoco.PrimaryKeyAttribute)atr; tbl.PrimaryKeyNamesFromAttribute =pka.Value; tbl.IsAutoIncrement = tbl.IsAutoIncrement || pka.AutoIncrement; } if (atr is NPoco.TableNameAttribute) { var a = (NPoco.TableNameAttribute)atr; tbl.ClassName = t.Name; tbl.CleanName = a.Value; var props = t.GetProperties(); foreach (var p in props) { var col = new Column(); Attribute[] propAttributes = Attribute.GetCustomAttributes(p); foreach (Attribute patr in propAttributes) { if (patr is StringLengthAttribute) { col.StringLength = ((StringLengthAttribute)patr).MaximumLength; } if (patr is NPoco.ColumnAttribute) { var ca = patr as NPoco.ColumnAttribute; col.Name = String.IsNullOrEmpty(ca.Name) ? p.Name : ca.Name; col.PropertyName = p.Name; col.PropertyType = TypeMap.CommonSystemTypeFromFrameworkType(p.PropertyType.ToString()); col.IsNullable = col.PropertyType.EndsWith("?"); col.IsAutoIncrement = col.IsPK && tbl.IsAutoIncrement; } if (patr is EquivalentTypeForTestingAttribute) { var ca = patr as EquivalentTypeForTestingAttribute; col.PropertyType = ca.Name; col.IsNullable = col.PropertyType.EndsWith("?"); } } if (!String.IsNullOrEmpty(col.PropertyName)) { tbl.Columns.Add(col); } } } } if (!String.IsNullOrEmpty(tbl.ClassName)) { // Assign Primary Key(s) if found if (!String.IsNullOrEmpty(tbl.PrimaryKeyNamesFromAttribute)) { string[] pkNames = tbl.PrimaryKeyNamesFromAttribute.Split(',').Select(a => a.Trim()).ToArray(); var cols = tbl.Columns.Where(a => pkNames.Contains(a.PropertyName)); foreach (var col in cols) { col.IsPK = true; col.IsAutoIncrement = tbl.IsAutoIncrement; } } tbls.Add(tbl); } } currentPocos = tbls; }