//6.0 TODO: Merge into ICollectionPropertiesMapper
        public static void Type(
            this ICollectionPropertiesMapper mapper,
            string collectionType)
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            switch (mapper)
            {
            case BagMapper bagMapper:
                bagMapper.Type(collectionType);
                break;

            case IdBagMapper idBagMapper:
                idBagMapper.Type(collectionType);
                break;

            case ListMapper listMapper:
                listMapper.Type(collectionType);
                break;

            case MapMapper mapMapper:
                mapMapper.Type(collectionType);
                break;

            case SetMapper setMapper:
                setMapper.Type(collectionType);
                break;

            default:
                throw new NotSupportedException($"{mapper.GetType().FullName} does not support Type(string)");
            }
        }
        /// <summary>
        /// Sets the following conventions:
        /// 1) Foreign key fields are named as the property name suffixed by the value of _foreignKeyColumnSuffix.
        /// 2) Many to Many link tables are named as the object type names sorted alphabetically with the _manyToManyLinkTableInsert inserted inbetween them.
        /// </summary>
        private void BeforeMappingCollectionConvention(
            IModelInspector inspector,
            PropertyPath member,
            ICollectionPropertiesMapper customizer)
        {
            string tableName;

            if (inspector.IsManyToMany(member.LocalMember))
            {
                tableName = this.GetManyToManyLinkTableName(member);
                customizer.Table(tableName);
            }
            else
            {
                tableName = member.GetCollectionElementType().Name;
            }

            string columnName     = this.GetKeyColumnName(inspector, member);
            string foreignKeyName = $"{this._foreignKeyNamePrefix}{tableName}_{columnName}";

            customizer.Key(
                k =>
            {
                k.Column(columnName);
                k.ForeignKey(foreignKeyName);
            });
        }
        private void BeforeMappingCollectionConvention(IModelInspector inspector, PropertyPath member, ICollectionPropertiesMapper customizer)
        {
            if (inspector.IsManyToMany(member.LocalMember))
                customizer.Table(member.ManyToManyIntermediateTableName());

            customizer.Key(k => k.Column(DetermineKeyColumnName(inspector, member)));
        }
 //6.0 TODO: Merge into ICollectionPropertiesMapper<TEntity, TElement>
 public static void Type <TEntity, TElement>(
     this ICollectionPropertiesMapper <TEntity, TElement> mapper,
     string collectionType)
 {
     ReflectHelper
     .CastOrThrow <CollectionPropertiesCustomizer <TEntity, TElement> >(mapper, "Type(string)")
     .Type(collectionType);
 }
Exemple #5
0
        private static void OnBeforeMapCollection(IModelInspector inspector, PropertyPath member,
                                                  ICollectionPropertiesMapper customizer)
        {
            customizer.Key(mapper =>
            {
                mapper.Column(columnMapper => columnMapper.Name(member.GetContainerEntity(inspector).Name + "Id"));
                if (inspector.IsManyToManyItem(member.LocalMember))
                {
                    mapper.ForeignKey(
                        $"FK_{member.GetContainerEntity(inspector).Name.ToUpper()}_{member.LocalMember.Name.ToUpper()}_{member.GetContainerEntity(inspector).Name + "Id"}");
                }
                else
                {
                    var a = member.LocalMember.GetPropertyOrFieldType();
                    if (a.IsEnumerable(out var elementType))
                    {
                        mapper.ForeignKey(
                            $"FK_{elementType.Name.ToUpper()}_{member.GetContainerEntity(inspector).Name.ToUpper()}");
                    }
                }
            });

            if (inspector.IsManyToManyItem(member.LocalMember))
            {
                customizer.Table(member.LocalMember.DeclaringType.Name + "_" + member.LocalMember.Name);
            }
            else
            {
                if (member.LocalMember.GetPropertyOrFieldType().IsEnumerable(out var elementType) &&
                    typeof(Entity).IsAssignableFrom(elementType))
                {
                    var parentRef = elementType
                                    .GetProperties()
                                    .Single(p => p.PropertyType == member.GetContainerEntity(inspector));

                    customizer.Key(o => o.Column($"{parentRef.Name}Id"));
                    customizer.Inverse(true);
                }

                customizer.Cascade(Cascade.All | Cascade.DeleteOrphans);
            }
        }
        /// <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);
            });
        }
 public static void FilterSoftDeleted <TEntity, TNested>(this ICollectionPropertiesMapper <TEntity, TNested> mapper)
     where TEntity : class
     where TNested : class
 {
     mapper.Where("[State] = 'Normal'");
 }
        protected virtual void OnBeforeMappingCollectionConvention(IModelInspector modelinspector, PropertyPath member, ICollectionPropertiesMapper collectionPropertiesCustomizer)
        {
            if (modelinspector.IsManyToMany(member.LocalMember))
            {
                collectionPropertiesCustomizer.Table(member.ManyToManyIntermediateTableName("To"));
            }

            if (modelinspector.IsSet(member.LocalMember))
            {
                // If otherside has many-to-one, make it inverse, if not specify foreign key on Key element
                MemberInfo oneToManyProperty = member.OneToManyOtherSideProperty();
                IEnumerable <MemberInfo> candidatesManyToOne =
                    MembersProvider
                    .GetRootEntityMembers(oneToManyProperty.DeclaringType)
                    .Where(modelinspector.IsManyToOne);
                if (candidatesManyToOne.Any(mi => mi.MemberType() == member.LocalMember.DeclaringType))
                {
                    collectionPropertiesCustomizer.Inverse(true);
                }
                else
                {
                    Contract.Assert(oneToManyProperty.DeclaringType != null, "otherSideProperty.DeclaringType != null");
                    collectionPropertiesCustomizer.Key(k => k.ForeignKey(string.Format("FK_{0}_{1}", oneToManyProperty.DeclaringType.Name, oneToManyProperty.Name)));
                }
            }

            collectionPropertiesCustomizer.Key(k => k.Column(GetKeyColumnName(modelinspector, member)));
        }
Exemple #9
0
        private void OneToManyConvention(IModelInspector modelInspector, PropertyPath member, ICollectionPropertiesMapper map)
        {
            var inv = GetInverseProperty(member.LocalMember);

            if (inv == null && InverseFkColumnNaming != null)
            {
                var containerEntity = member.GetContainerEntity(modelInspector);
                foreach (var idProperty in containerEntity.GetProperties().Where(modelInspector.IsPersistentId))
                {
                    map.Key(x => x.Column(InverseFkColumnNaming(member.LocalMember, containerEntity, idProperty)));
                }
            }
        }
 private void DefaultMapCollection(IModelInspector inspector, PropertyPath member, ICollectionPropertiesMapper customizer)
 {
     customizer.Key(k => k.Column(DetermineKeyColumnName(inspector, member)));
 }
        private void BeforeMappingCollectionConvention(IModelInspector inspector, PropertyPath member, ICollectionPropertiesMapper customizer)
        {
            if (inspector.IsManyToMany(member.LocalMember))
            {
                customizer.Table(member.ManyToManyIntermediateTableName());
            }

            customizer.Key(k => k.Column(DetermineKeyColumnName(inspector, member)));
        }
        protected virtual void OnBeforeMappingCollectionConvention(IModelInspector modelinspector, PropertyPath member, ICollectionPropertiesMapper collectionPropertiesCustomizer)
        {
            if (modelinspector.IsManyToMany(member.LocalMember))
            {
                collectionPropertiesCustomizer.Table(member.ManyToManyIntermediateTableName("To"));
            }

            if (modelinspector.IsSet(member.LocalMember))
            {
                // If otherside has many-to-one, make it inverse, if not specify foreign key on Key element
                MemberInfo oneToManyProperty = member.OneToManyOtherSideProperty();
                IEnumerable<MemberInfo> candidatesManyToOne =
                    MembersProvider
                        .GetRootEntityMembers(oneToManyProperty.DeclaringType)
                        .Where(modelinspector.IsManyToOne);
                if (candidatesManyToOne.Any(mi => mi.MemberType() == member.LocalMember.DeclaringType))
                {
                    collectionPropertiesCustomizer.Inverse(true);
                }
                else
                {
                    Contract.Assert(oneToManyProperty.DeclaringType != null, "otherSideProperty.DeclaringType != null");
                    collectionPropertiesCustomizer.Key(k => k.ForeignKey(string.Format("FK_{0}_{1}", oneToManyProperty.DeclaringType.Name, oneToManyProperty.Name)));
                }
            }

            collectionPropertiesCustomizer.Key(k => k.Column(GetKeyColumnName(modelinspector, member)));
        }
        /// <summary>
        /// Sets the following conventions:
        /// 1) Foreign key fields are named as the property name suffixed by the value of _foreignKeyColumnSuffix.
        /// 2) Many to Many link tables are named as the object type names sorted alphabetically with the _manyToManyLinkTableInsert inserted inbetween them.
        /// </summary>
        private void BeforeMappingCollectionConvention(IModelInspector inspector, PropertyPath member, ICollectionPropertiesMapper customizer)
        {
            string tableName;
            if (inspector.IsManyToMany(member.LocalMember))
            {
                tableName = GetManyToManyLinkTableName(member);
                customizer.Table(tableName);
            }
            else
            {
                tableName = member.GetCollectionElementType().Name;
            }

            string columnName = GetKeyColumnName(inspector, member);
            string foreignKeyName = string.Format("{0}{1}_{2}", _foreignKeyNamePrefix, tableName, columnName);
            customizer.Key(k =>
            {
                k.Column(columnName);
                k.ForeignKey(foreignKeyName);
            });
        }
Exemple #14
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.IsManyToManyItem(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);
            });
        }
Exemple #15
0
 private static void InverseIfBidirectional(IModelInspector modelinspector, PropertyPath member, ICollectionPropertiesMapper propertycustomizer)
 {
     propertycustomizer.Inverse(BidirectionAssociation.AnalyzeManyToOne(member).Inverse);
 }
Exemple #16
0
 private static void CascadeIfSetOfNonAggregateRoots(IModelInspector modelinspector, PropertyPath member, ICollectionPropertiesMapper propertycustomizer)
 {
     propertycustomizer.Cascade(BidirectionAssociation.AnalyzeManyToOne(member).Cascade);
 }