Thanks for the link. I'll look at it. Trying to write a save event handler on the entitymanager which trims leading/trailing characters of all the varchar() fields and Nulls those that support it. Just a generic safety. The problem is that some of the fields are char() and shouldn't be trimmed.
for example... right now I have any string[6] hardcoded to not be trimmed... Not elegant but functional for our application.
private static void TrimStringProperties(IEnumerable<object> entities) { foreach (Entity entity in entities) { var properties = entity.GetType().GetProperties().Where(p => p.PropertyType == typeof(string)); foreach (var property in properties) { var attributeNames = property.GetCustomAttributes(false).Select(a => a.ToString()).ToList(); if (!attributeNames.Any(a => a.Contains("DataMemberAttribute"))) continue; var isNullable = ((IBaseEntity)entity).IsNullable(property.Name); var maxLength = ((IBaseEntity)entity).MaxStringLength(property.Name); if (maxLength == 6) continue; // fixed length char(6) properties
var value = (string)property.GetValue(entity, null); if (value == null) continue;
value = value.Trim(); if (isNullable && string.IsNullOrEmpty(value)) value = null; property.SetValue(entity, value, null); } } }
Another suggestion is to do the trimming on a varchar on the SQL database. You can use SQL_Variant_PROPERTY function to determine the field type and LTrim or RTrim function to do the trimming.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum