/// <summary> /// Sets the mapper to use: /// 1) a properties StringLength attribute if it has one for the databases field size. /// 2) non-nullable types to be not nullable in the database. /// 3) creates indexes based on the index attributes of properties. /// </summary> private void OnMapperOnBeforeMapProperty( IModelInspector inspector, PropertyPath member, IPropertyMapper customizer) { // Get all the custom attributes. var customAttributes = member.LocalMember.GetCustomAttributes(false); // For all types check for index attributes and add indexes if required. var indexAttributes = customAttributes.OfType <IndexAttribute>(); foreach (var indexAttribute in indexAttributes) { string indexPrefix = member.GetContainerEntity(inspector).Name; if (indexAttribute.Unique) { string indexName = $"UI_{indexPrefix}_{indexAttribute.Name}"; customizer.UniqueKey(indexName); } else { string indexName = $"IX_{indexPrefix}_{indexAttribute.Name}"; customizer.Index(indexName); } } // For string types check for string length attribute and set field length if required Type memberType = member.LocalMember.GetPropertyOrFieldType(); if (memberType == typeof(string)) { StringLengthAttribute stringlengthAttribute = (StringLengthAttribute) customAttributes.FirstOrDefault(x => x.GetType() == typeof(StringLengthAttribute)); int length = this.DefaltStringLength; if (stringlengthAttribute != null && stringlengthAttribute.MaximumLength > 0) { length = stringlengthAttribute.MaximumLength; } customizer.Length(length); } // For all types if the type is not nullable then set not nullable to true. if (!IsNullable(memberType)) { customizer.NotNullable(true); } }
/// <summary> /// Sets the mapper to use: /// 1) a properties StringLength attribute if it has one for the databases field size. /// 2) non-nullable types to be not nullable in the database. /// 3) creates indexes based on the index attributes of properties. /// </summary> private void OnMapperOnBeforeMapProperty(IModelInspector inspector, PropertyPath member, IPropertyMapper customizer) { // Get all the custom attributes. var customAttributes = member.LocalMember.GetCustomAttributes(false); // For all types check for index attributes and add indexes if required. var indexAttributes = customAttributes.OfType<IndexAttribute>(); foreach (var indexAttribute in indexAttributes) { string indexPrefix = member.GetContainerEntity(inspector).Name; if (indexAttribute.Unique) { string indexName = string.Format("UI_{0}_{1}", indexPrefix, indexAttribute.Name); customizer.UniqueKey(indexName); } else { string indexName = string.Format("IX_{0}_{1}", indexPrefix, indexAttribute.Name); customizer.Index(indexName); } } // For string types check for string length attribute and set field length if required Type memberType = member.LocalMember.GetPropertyOrFieldType(); if (memberType == typeof(string)) { StringLengthAttribute stringlengthAttribute = (StringLengthAttribute)customAttributes.FirstOrDefault(x => x.GetType() == typeof(StringLengthAttribute)); int length = DefaltStringLength; if (stringlengthAttribute != null && stringlengthAttribute.MaximumLength > 0) { length = stringlengthAttribute.MaximumLength; } customizer.Length(length); } // For all types if the type is not nullable then set not nullable to true. if (!IsNullable(memberType)) { customizer.NotNullable(true); } }