private static void SetupIfIdentity(PropertyInfo property, DommelPropertyMap map) { if (property.GetCustomAttributes(typeof(DatabaseGeneratedAttribute)) .FirstOrDefault() is DatabaseGeneratedAttribute dbGenerated) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse // ReSharper disable once UseNullPropagation if (dbGenerated == null) { return; } if (dbGenerated.DatabaseGeneratedOption != DatabaseGeneratedOption.Identity) { return; } map.IsIdentity(); _fastCrudMap .SetProperty(property.Name, prop => prop.SetPrimaryKey() .SetDatabaseGenerated(DatabaseGeneratedOption.Identity)); } else { //assume database generated and pk _fastCrudMap .SetProperty(property.Name, prop => prop.SetPrimaryKey() .SetDatabaseGenerated(DatabaseGeneratedOption.Identity)); } }
public LinqPropertyMap(DommelPropertyMap map) { Guard.ArgumentNotNull(map, "map"); _innerMap = map; Type = LookupDbType(map.PropertyInfo.PropertyType); }
static CustomEntityMap() { if (Attribute.IsDefined(typeof(TEntity), typeof(TableAttribute))) { _defaultTableName = ((TableAttribute)typeof(TEntity).GetCustomAttribute(typeof(TableAttribute))) .Name; } if (Attribute.IsDefined(typeof(TEntity), typeof(SchemaAttribute))) { _defaultSchemaName = ((SchemaAttribute)typeof(TEntity).GetCustomAttribute(typeof(SchemaAttribute))) .Name; } _propertyKeyMappings = new Dictionary <Type, KeyType> { { typeof(byte), KeyType.Identity }, { typeof(byte?), KeyType.Identity }, { typeof(sbyte), KeyType.Identity }, { typeof(sbyte?), KeyType.Identity }, { typeof(short), KeyType.Identity }, { typeof(short?), KeyType.Identity }, { typeof(ushort), KeyType.Identity }, { typeof(ushort?), KeyType.Identity }, { typeof(int), KeyType.Identity }, { typeof(int?), KeyType.Identity }, { typeof(uint), KeyType.Identity }, { typeof(uint?), KeyType.Identity }, { typeof(long), KeyType.Identity }, { typeof(long?), KeyType.Identity }, { typeof(ulong), KeyType.Identity }, { typeof(ulong?), KeyType.Identity }, { typeof(BigInteger), KeyType.Identity }, { typeof(BigInteger?), KeyType.Identity }, { typeof(Guid), KeyType.Guid }, { typeof(Guid?), KeyType.Guid } }; // get the writable public properties _properties = typeof(TEntity) .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(m => m.GetSetMethod(false) != null) // ignore .Where(m => !m.GetCustomAttributes(typeof(NotMappedAttribute)).Any()) .Where(m => !m.GetCustomAttributes(typeof(IgnoreDataMemberAttribute)).Any()) .ToArray(); var validProperties = _properties.Select(m => m.Name).Distinct().ToList(); var descriptors = TypeDescriptor.GetProperties(typeof(TEntity)); _propertyDescriptors = descriptors .OfType <PropertyDescriptor>() .Where(m => validProperties.Contains(m.Name)) .ToArray(); _tableName = _defaultTableName ?? typeof(TEntity).Name.Pluralize(); // dapper type mapping wrap into custom type map var typeMap = SqlMapper.GetTypeMap(typeof(TEntity)); _customTypeMap = new CustomTypeMap <TEntity>(typeMap); // fast crud registration _fastCrudMap = OrmConfiguration.RegisterEntity <TEntity>() .SetTableName(_tableName); AdaptToFastCrud(_fastCrudMap, _customTypeMap); _linqPropertyMaps = new List <IPropertyMap>(); _propertyMaps = new List <DommelPropertyMap>(); // add all other properties properties foreach (var property in _properties) { var isKey = property.GetCustomAttributes(typeof(KeyAttribute)).Any(); // not already assigned var columnName = property.GetCustomAttributes(typeof(ColumnAttribute)) .Select(q => ((ColumnAttribute)q).Name) .FirstOrDefault() ?? property.Name; // default to property name var map = new DommelPropertyMap(property); map.ToColumn(columnName); var linkMap = new LinqPropertyMap(map); if (isKey || property.Name == "Id") { var keyAssignmentType = _propertyKeyMappings.ContainsKey(linkMap.PropertyInfo.PropertyType) ? _propertyKeyMappings[linkMap.PropertyInfo.PropertyType] : KeyType.Assigned; map.IsKey(); SetupIfIdentity(property, map); linkMap.Key(keyAssignmentType); } _propertyMaps.Add(map); //cascade column mapping to dapper property maps _customTypeMap.Map(property.Name, columnName); linkMap.Key(KeyType.Assigned); _linqPropertyMaps.Add(linkMap); } }