public virtual TModel BuildFrom(IDbReader reader) { if (!reader.Read()) { return(null); } var model = new TModel(); Properties.ForEach(property => { if (!reader.IsDbNull(property.Name)) { property.SetValue(model, reader[property.Name], null); } }); ManyToOneProperties.ForEach(property => { var propertyName = property.Name + "Id"; if (!reader.IsDbNull(propertyName)) { var manyToOneObject = Activator.CreateInstance(property.PropertyType); var idProperty = property.PropertyType.GetRuntimeProperty("Id"); idProperty.SetValue(manyToOneObject, reader[propertyName], null); property.SetValue(model, manyToOneObject, null); } }); return(model); }
private void AddManyToOneRecords(TModel model, IDictionary <string, object> dictionary) { ManyToOneProperties.ForEach(propertyInfo => { var dbColumnName = propertyInfo.Name + "Id"; var manyToOneObject = propertyInfo.GetValue(model); if (manyToOneObject == null) { if (!dictionary.ContainsKey(dbColumnName)) { dictionary.Add(dbColumnName, null); } return; } var manyToOneObjectType = manyToOneObject.GetType(); var idPropertyInfo = manyToOneObjectType.GetRuntimeProperty("Id"); var idValue = idPropertyInfo.GetValue(manyToOneObject, null); if (idPropertyInfo.PropertyType == typeof(Guid)) { if ((Guid)idValue == Guid.Empty) { idValue = null; } } else if (idPropertyInfo.PropertyType == typeof(int)) { if ((int)idValue == 0) { idValue = null; } } else if (idPropertyInfo.PropertyType == typeof(long)) { if ((long)idValue == 0) { idValue = null; } } if (!dictionary.ContainsKey(dbColumnName)) { dictionary.Add(dbColumnName, idValue); } }); }
public virtual TModel BuildFrom(IDataReader reader) { if (!reader.Read()) { return(null); } var model = new TModel(); Properties.ForEach(property => { var ordinal = reader.GetOrdinal(property.Name); if (reader.IsDBNull(ordinal)) { return; } SetPropertyValue(ref property, model, reader, ordinal); }); ManyToOneProperties.ForEach(property => { var propertyName = property.Name + "Id"; var ordinal = reader.GetOrdinal(propertyName); if (!reader.IsDBNull(ordinal)) { var manyToOneObject = Activator.CreateInstance(property.PropertyType); var idProperty = property.PropertyType.GetRuntimeProperty("Id"); SetPropertyValue(ref idProperty, manyToOneObject, reader, ordinal); property.SetValue(model, manyToOneObject, null); } }); return(model); }