/// <summary> /// Updates the key propery. /// </summary> /// <param name="Record">The record.</param> /// <param name="properties">The properties.</param> /// <param name="result">The result.</param> private static void UpdateKeyPropery(IDataEntity Record, List <PropertyInfo> properties, int result) { foreach (PropertyInfo property in properties) { DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault(); if (KeyProperty != null) { property.SetValue(Record, result); } } }
private static void CreateUpdateQuery(IDataEntity Record, StringBuilder Query, List <PropertyInfo> properties, Dictionary <string, string> FieldsNames) { List <string> FieldsHolders = new List <string>(); List <string> QueryParts = new List <string>(); foreach (PropertyInfo property in properties) { //first we dfo not want to add in the insert statement the Key var KeyName = (from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault(); var NotInsert = (from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.NotInsert) select attr).FirstOrDefault(); if (NotInsert == null) { if (KeyName == null) { //check if the property has mapped field attribute //if the attribute is missing we use the property name as field DataInterface.Attribbutes.Field FieldAttribute = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault(); if (FieldAttribute == null) { FieldAttribute = new DataInterface.Attribbutes.Field(property.Name); } FieldsNames.Add(property.Name, FieldAttribute.Name); FieldsHolders.Add($"@{FieldAttribute.Name}"); QueryParts.Add($"{FieldAttribute.Name}=@{FieldAttribute.Name}"); } } } Query.Append(String.Join(",", QueryParts.ToArray())); foreach (PropertyInfo property in properties) { DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault(); if (KeyProperty != null) { //find if the property has a field attribute //if not we map to the property;'s name DataInterface.Attribbutes.Field FieldProperty = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault(); if (FieldProperty != null) { Query.Append($" where {FieldProperty.Name }=@{FieldProperty.Name}"); } else { Query.Append($" where {property.Name}=@{property.Name}"); } } } }
/// <summary> /// Updates the record. /// If there is a property marked as EntityKey the update will consider that field as Unique Key /// If there is not any property marked as EntityKey the function will throw an exception /// </summary> /// <param name="record">The record.</param> /// <returns></returns> /// <exception cref="System.Exception"> /// There is not any property marked as EntityKey /// or /// A Class marked with attribute NotInsert cannot perform write opperations /// </exception> public bool UpdateRecord(IDataEntity record) { List <PropertyInfo> properties = record.GetType().GetProperties().ToList(); Dictionary <string, string> FieldsNames = new Dictionary <string, string>(); var customAttributes = record.GetType().GetCustomAttributes(); OnBeforeInsert(null); //find if there is an EntityKey property. If there is not we abort the operation Boolean KeyExists = false; foreach (PropertyInfo property in properties) { DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault(); if (KeyProperty != null) { KeyExists = true; break; } } if (KeyExists == false) { throw new Exception("There is not any property marked as EntityKey"); } DataInterface.Attribbutes.NotInsert NotInsertAttribute = (DataInterface.Attribbutes.NotInsert)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.NotInsert) select attr).FirstOrDefault(); if (NotInsertAttribute != null) { throw new Exception("A Class marked with attribute NotInsert cannot perform write opperations"); } //first find the table attribute to obtain the maped db table DataInterface.Attribbutes.Table TableAttribute = (DataInterface.Attribbutes.Table)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Table) select attr).FirstOrDefault(); using (SqlConnection connection = new SqlConnection(this.Source)) { connection.Open(); try { StringBuilder Query = new StringBuilder(); if (TableAttribute != null) { Query.Append($"Update {TableAttribute.Name} set "); } else { Query.Append($"Update {record.GetType().Name} set"); } //get the properties of the entity // create the insert query CreateUpdateQuery(record, Query, properties, FieldsNames); var com = new SqlCommand(Query.ToString(), connection); //Create the sql parameters if (_Encryptor != null) { if (record is IEncryptableClass) { _Encryptor.EncryptProperties((IEncryptableClass)record); } } SetInsetParameters(record, properties, FieldsNames, com); foreach (PropertyInfo property in properties) { DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault(); if (KeyProperty != null) { DataInterface.Attribbutes.Field FieldProperty = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault(); if (FieldProperty != null) { com.Parameters.AddWithValue($"@{FieldProperty.Name}", property.GetValue(record)); } else { com.Parameters.AddWithValue($"@{property.Name}", property.GetValue(record)); } } } com.ExecuteNonQuery(); //find the Key property of the entity if (_Encryptor != null) { if (record is IEncryptableClass) { _Encryptor.Decryptproperties((IEncryptableClass)record); } } connection.Close(); } catch (Exception ex) { if (connection.State == System.Data.ConnectionState.Open) { connection.Close(); } throw; } } return(true); }