private string KeyCol() { string columna = ""; if (GetClassAttribute().IsMappedByLabels) { object exFormAsObj = Activator.CreateInstance(typeof(T)); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = exFormAsObj.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (hiddenAttribute.IsKey) { columna = hiddenAttribute.Name; } } } else { object exFormAsObj = Activator.CreateInstance(typeof(T)); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = exFormAsObj.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (hiddenAttribute.IsKey) { columna = prop.Name; } } } return(columna); }
private async Task <bool> AddDefaultColumnsToProject(int projectId) { var project = await _databaseContext.Projects.FindAsync(projectId); var column1 = new ColumnDB() { Title = "To Do", Project = project, Tasks = new List <ProjectTaskDB>() }; var column2 = new ColumnDB() { Title = "In Progress", Project = project, Tasks = new List <ProjectTaskDB>() }; var column3 = new ColumnDB() { Title = "Done", Project = project, Tasks = new List <ProjectTaskDB>() }; await _databaseContext.Columns.AddAsync(column1); await _databaseContext.Columns.AddAsync(column2); await _databaseContext.Columns.AddAsync(column3); await _databaseContext.SaveChangesAsync(); return(true); }
private bool ActionsObjectCode(DbManagerTypes dbManagerTypes, TableDB tableDefinifiton) { //TableDB tableDefinifiton = GetClassAttribute(); bool result = false; //mapeo de tabla con los nombres de los campos ya existentes string sentencia = ""; string sentenciaVariables = ""; foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (hiddenAttribute == null) { throw new Exceptions.DarkException(string.Format("The attribute was not found in the attribute '{0}', if you don´t want to use mapTable, please set IsMappedByLabels = false", prop.Name)); } if (tableDefinifiton.IsMappedByLabels) { if (string.IsNullOrEmpty(hiddenAttribute.Name)) { throw new Exceptions.DarkException(string.Format("The attribute {0} was setting like mapping column, the name is missing", prop.Name)); } } else { hiddenAttribute.Name = prop.Name; } if (dbManagerTypes == DbManagerTypes.Add) { if (!hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { sentencia += hiddenAttribute.Name + ","; sentenciaVariables += "@" + hiddenAttribute.Name + ","; } } else if (dbManagerTypes == DbManagerTypes.Update) { if (!hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { sentencia += hiddenAttribute.Name + " = @" + hiddenAttribute.Name + ","; } else if (hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { sentenciaVariables = hiddenAttribute.Name + " = @" + hiddenAttribute.Name + ""; } else { } } else if (dbManagerTypes == DbManagerTypes.Delete) { if (hiddenAttribute.IsKey) { sentenciaVariables = hiddenAttribute.Name + " = @" + hiddenAttribute.Name + ""; } } else { throw new Exceptions.DarkException(string.Format("Delete action is not active")); } } if (dbManagerTypes == DbManagerTypes.Add) { string Statement = string.Format("INSERT INTO {0}({1}) VALUES({2})", Nametable, sentencia.Substring(0, sentencia.Length - 1), sentenciaVariables.Substring(0, sentenciaVariables.Length - 1)); List <ProcedureModel> procedureModels = new List <ProcedureModel>(); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (!hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { procedureModels.Add(new ProcedureModel { Namefield = tableDefinifiton.IsMappedByLabels ? hiddenAttribute.Name : prop.Name, value = propertyInfo.GetValue(Element) }); } } dBConnection.StartInsert(Statement, procedureModels); result = true; } else if (dbManagerTypes == DbManagerTypes.Update) { string Statement = string.Format("UPDATE {0} SET {1} WHERE {2} ", Nametable, sentencia.Substring(0, sentencia.Length - 1), sentenciaVariables); List <ProcedureModel> procedureModels = new List <ProcedureModel>(); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (hiddenAttribute.IsMapped) { procedureModels.Add(new ProcedureModel { Namefield = tableDefinifiton.IsMappedByLabels ? hiddenAttribute.Name : prop.Name, value = propertyInfo.GetValue(Element) }); } } dBConnection.StartUpdate(Statement, procedureModels); result = true; } else if (dbManagerTypes == DbManagerTypes.Delete) { string Statement = string.Format("DELETE FROM {0} WHERE {1} ", Nametable, sentenciaVariables); List <ProcedureModel> procedureModels = new List <ProcedureModel>(); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (hiddenAttribute.IsMapped && hiddenAttribute.IsKey) { procedureModels.Add(new ProcedureModel { Namefield = tableDefinifiton.IsMappedByLabels ? hiddenAttribute.Name : prop.Name, value = propertyInfo.GetValue(Element) }); } } dBConnection.StartDelete(Statement, procedureModels); result = true; } else { throw new Exceptions.DarkException(string.Format("Delete action is not active")); } return(result); }
private List <T> DataReader(string SqlStatements) { TableDB tableDefinifiton = GetClassAttribute(); System.Data.SqlClient.SqlDataReader Data = dBConnection.GetDataReader(SqlStatements); List <T> Response = new List <T>(); while (Data.Read()) { object exFormAsObj = Activator.CreateInstance(typeof(T)); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = exFormAsObj.GetType().GetProperty(prop.Name); ColumnDB hiddenAttribute = (ColumnDB)propertyInfo.GetCustomAttribute(typeof(ColumnDB)); if (hiddenAttribute == null) { throw new Exceptions.DarkException(string.Format("The attribute was not found in the attribute '{0}', if you don´t want to use mapTable, please set IsMappedByLabels = false", prop.Name)); } if (hiddenAttribute.IsMapped) { string NombrePropiedad = ""; if (tableDefinifiton.IsMappedByLabels) { NombrePropiedad = hiddenAttribute.Name.Trim(); } else { NombrePropiedad = prop.Name; } try { if (prop.PropertyType.Equals(typeof(DateTime))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? DateTime.Now : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.DateTime), null); } if (prop.PropertyType.Equals(typeof(DateTime?))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? null : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, value, null); } if (prop.PropertyType.Equals(typeof(TimeSpan))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? null : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, value, null); } if (prop.PropertyType.Equals(typeof(double))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.Double), null); } if (prop.PropertyType.Equals(typeof(float))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ToSingle(value), null); } if (prop.PropertyType.Equals(typeof(Decimal))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.Double), null); } if (prop.PropertyType.Equals(typeof(string))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? "" : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.String), null); } if (prop.PropertyType.Equals(typeof(bool))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? false : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.Boolean), null); } if (prop.PropertyType.Equals(typeof(int))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, propertyInfo.PropertyType), null); } } catch (Exception ex) { throw new Exceptions.DarkException(string.Format("The attribute'{0}' has an error, {1}", prop.Name, ex.Message)); } } } Response.Add((T)exFormAsObj); } Data.Close(); return(Response); }