Exemplo n.º 1
0
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.Class <Project>(map => {
                map.Set(x => x.Contributors, set => {
                    set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), UserMapping.Contributor, typeof(User)));
                });

                map.Set(x => x.Administrators, set => {
                    set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), UserMapping.Administrator, typeof(User)));
                });
            });
        }
Exemplo n.º 2
0
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.Class <User>(map => {
                map.Set(x => x.ContributorTo, set => {
                    set.Key(key => {
                        key.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <Project>(x => x.Contributors), typeof(Project)));
                    });
                    set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), Contributor, typeof(User)));
                }, r => r.ManyToMany(mtm => {
                    mtm.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <User>(x => x.ContributorTo), typeof(User)));
                }));

                map.Set(x => x.AdministratorOf, set => {
                    set.Key(key => {
                        key.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <Project>(x => x.Administrators), typeof(Project)));
                    });
                    set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), Administrator, typeof(User)));
                }, r => r.ManyToMany(mtm => {
                    mtm.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <User>(x => x.AdministratorOf), typeof(User)));
                }));
            });
        }
Exemplo n.º 3
0
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.IsManyToMany((member, declared) => {
                var property = member as PropertyInfo;

                if (property == null || !property.CanRead)
                {
                    return(false);
                }

                if (property.GetCustomAttribute <ManyToManyAttribute>() != null)
                {
                    return(true);
                }

                return(false);
            });

            mapper.BeforeMapSet += (modelInspector, propertyPath, propertyCustomizer) => {
                var manyToManyAttrib = propertyPath.LocalMember.GetCustomAttribute <ManyToManyAttribute>();
                if (manyToManyAttrib == null)
                {
                    return;
                }

                if (manyToManyAttrib.IsActiveSide)
                {
                    propertyCustomizer.Cascade(Cascade.Persist);
                    propertyCustomizer.Inverse(false);
                }
                else
                {
                    propertyCustomizer.Cascade(Cascade.None);
                    propertyCustomizer.Inverse(true);
                }

                propertyCustomizer.Access(typeof(SourceCollectionAccessor));

                var activeEntityType   = GetActiveType(propertyPath.LocalMember, manyToManyAttrib.IsActiveSide);
                var inactiveEntityType = GetInactiveType(propertyPath.LocalMember, manyToManyAttrib.IsActiveSide);

                propertyCustomizer.Table(formatter.GetManyToManyTableName(activeEntityType, inactiveEntityType));

                propertyCustomizer.Key(keyMap => {
                    var parentType = GetDeclaringType(propertyPath.LocalMember);
                    var childType  = GetCollectionType(propertyPath.LocalMember);

                    var columnName = formatter.GetIdentityColumnName(parentType);
                    var fkName     = formatter.GetForeignKeyConstraintName(activeEntityType, inactiveEntityType);

                    keyMap.Column(columnName);
                    keyMap.ForeignKey(fkName);
                });
            };

            mapper.BeforeMapManyToMany += (modelInspector, propertyPath, collectionRelationManyToManyCustomizer) => {
                var myType       = GetDeclaringType(propertyPath.LocalMember);
                var oppositeType = GetCollectionType(propertyPath.LocalMember);

                collectionRelationManyToManyCustomizer.Column(formatter.GetIdentityColumnName(oppositeType));
                collectionRelationManyToManyCustomizer.ForeignKey(formatter.GetForeignKeyConstraintName(oppositeType, myType));
            };
        }