コード例 #1
0
 public static void ManyToManyConvention(IModelInspector modelInspector, PropertyPath member, IManyToManyMapper map)
 {
     map.ForeignKey(
         string.Format("fk_{0}_{1}",
                member.LocalMember.Name,
                member.GetContainerEntity(modelInspector).Name));
 }
コード例 #2
0
 public static void ReferenceConvention(IModelInspector modelInspector, PropertyPath member, IManyToOneMapper map)
 {
     map.Column(k => k.Name(member.LocalMember.GetPropertyOrFieldType().Name + "Id"));
     map.ForeignKey(
         string.Format("fk_{0}_{1}",
                member.LocalMember.Name,
                member.GetContainerEntity(modelInspector).Name));
     map.Cascade(Cascade.All | Cascade.DeleteOrphans);
 }
コード例 #3
0
 public static void OneToManyConvention(IModelInspector modelInspector, PropertyPath member, IBagPropertiesMapper map)
 {
     var inv = member.LocalMember.GetInverseProperty();
     if (inv == null)
     {
         map.Key(x => x.Column(member.GetContainerEntity(modelInspector).Name + "Id"));
         map.Cascade(Cascade.All | Cascade.DeleteOrphans);
         map.BatchSize(20);
         map.Inverse(true);
     }
 }
コード例 #4
0
        /// <summary>
        /// Maps a many to one relationship
        /// </summary>
        /// <param name="modelInspector">The model inspector</param>
        /// <param name="property">The property to map</param>
        /// <param name="mapper">The property mapper</param>
        private void MapManyToOne(IModelInspector modelInspector, PropertyPath property, IManyToOneMapper mapper)
        {
            Type targetEntityType = property.LocalMember.GetPropertyOrFieldType();
            Type sourceEntityType = property.GetContainerEntity(modelInspector);
            MemberInfo member = property.PreviousPath != null ? property.PreviousPath.LocalMember : property.LocalMember;

            var targetEntityIDProperty = modelInspector.GetIdentifierMember(targetEntityType);
            var foreignKeyProperty = property.LocalMember;

            string columnName = null;
            string foreignKeyName = null;

            if (MatchOneToOneComponent(property, modelInspector))
            {
                columnName = namingEngine.ToComponentForeignKeyColumnName(foreignKeyProperty, member, targetEntityIDProperty);
                foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, member, targetEntityIDProperty);
            }
            else
            {
                columnName = namingEngine.ToForeignKeyColumnName(property.LocalMember, targetEntityIDProperty);
                foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, foreignKeyProperty, targetEntityIDProperty);
            }

            mapper.Column(columnName);
            mapper.ForeignKey(foreignKeyName);
        }
コード例 #5
0
        /// <summary>
        /// Maps a collection of components or entities
        /// </summary>
        /// <param name="modelInspector">The model inspector</param>
        /// <param name="property">The property to map</param>
        /// <param name="mapper">The collections mapper</param>
        private void MapCollection(IModelInspector modelInspector, PropertyPath property, ICollectionPropertiesMapper mapper)
        {
            Type sourceType = property.GetContainerEntity(modelInspector);
            Type targetType = property.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType();

            var primaryKeyProperty = modelInspector.GetIdentifierMember(sourceType);
            var foreignKeyProperty = property.LocalMember;
            string foreignKeyColumnName = null;
            string foreignKeyName = null;
            string tableName = null;
            string schemaName = null;

            if (modelInspector.IsEntity(targetType))
            {
                // Entity Relationship Mapping
                if (modelInspector.IsManyToMany(property.LocalMember))
                {
                    // Many to many
                    foreignKeyColumnName = namingEngine.ToManyToManyForeignKeyColumnName(sourceType, primaryKeyProperty);
                    foreignKeyName = namingEngine.ToManyToManyForeignKeyName(sourceType, targetType, sourceType, primaryKeyProperty);
                    tableName = namingEngine.ToManyToManyTableName(sourceType, targetType);
                    schemaName = namingEngine.ToSchemaName(sourceType, targetType);
                }
                else
                {
                    // One to Many
                    foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty);
                    foreignKeyName = namingEngine.ToForeignKeyName(targetType, sourceType, sourceType, primaryKeyProperty);
                }
            }
            else if (IsElement(targetType))
            {
                // Element mapping
                foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty);
                foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty);
                tableName = namingEngine.ToElementTableName(sourceType, targetType, property.LocalMember);
                schemaName = namingEngine.ToSchemaName(sourceType, targetType);
            }
            else
            {
                // Component Relationship Mapping
                foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty);
                foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty);
                tableName = namingEngine.ToComponentTableName(sourceType, targetType, property.LocalMember);
                schemaName = namingEngine.ToSchemaName(sourceType, targetType);
            }

            // Mapping
            mapper.Schema(schemaName);
            mapper.Table(tableName);
            mapper.Key(k =>
            {
                k.Column(foreignKeyColumnName);
                k.ForeignKey(foreignKeyName);
            });
        }
 private static void MapperOnBeforeMapBag(IModelInspector insp, PropertyPath prop, IBagPropertiesMapper map)
 {
     map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));
     map.Cascade(Cascade.All);
 }
コード例 #7
0
        /// <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);
            }
        }