src/Inflatable/ClassMapper/Default/ManyToMany.cs
/// <summary>
/// Sets up the property (used internally)
/// </summary>
/// <param name="mappings">The mappings.</param>
/// <param name="sourceSpec">The source spec.</param>
/// <exception cref="ArgumentException">Foreign key IDs could not be found for {typeof(ClassType).Name}.{Name}</exception>
Sets up the property (used internally)
Sets up the property ( used internally )
public override void Setup(IMappingSource mappings, ISource sourceSpec)
        {
            ForeignMapping = mappings.GetChildMappings<DataType>()
                                     .SelectMany(x => mappings.GetParentMapping(x.ObjectType))
                                     .Where(x => x.IDProperties.Count > 0)
                                     .Distinct()
                                     .ToList();
            if (ForeignMapping is null)
            {
                throw new ArgumentException($"Foreign key IDs could not be found for {typeof(ClassType).Name}.{Name}");
            }
            var ParentMappings = mappings.GetChildMappings(ParentMapping.ObjectType).SelectMany(x => mappings.GetParentMapping(x.ObjectType)).Distinct();
            var ParentWithID = ParentMappings.FirstOrDefault(x => x.IDProperties.Count > 0);
            if (string.IsNullOrEmpty(TableName))
            {
                var Class1 = ParentWithID.ObjectType.Name;
                var Class2 = ForeignMapping.OrderBy(x => x.ObjectType.Name).FirstOrDefault()?.ObjectType.Name ?? string.Empty;
                if (string.CompareOrdinal(Class1, Class2) < 0)
                {
                    SetTableName(Class1 + "_" + Class2);
                }
                else
                {
                    SetTableName(Class2 + "_" + Class1);
                }
            }
            if (sourceSpec.Tables.Any(x => x.Name == TableName))
            {
                return;
            }
            var JoinTable = sourceSpec.AddTable(TableName ?? string.Empty, ParentMapping.SchemaName);
            JoinTable.AddColumn<long>("ID_", DbType.UInt64, 0, false, true, false, true, false);
            var ParentIDMappings = ParentMappings.SelectMany(x => x.IDProperties);
            DatabaseJoinsCascade = ForeignMapping.Any(TempMapping => !ParentMappings.Contains(TempMapping));
            var Prefix = string.Empty;
            if (ForeignMapping.Any(TempMapping => ParentWithID == TempMapping))
            {
                Prefix = "Parent_";
            }
            foreach (var ParentIDMapping in ParentIDMappings)
            {
                JoinTable.AddColumn<object>(Prefix + ParentIDMapping.ParentMapping.TableName + ParentIDMapping.ColumnName,
                                ParentIDMapping.PropertyType.To(DbType.Int32),
                                ParentIDMapping.MaxLength,
                                false,
                                false,
                                false,
                                false,
                                false,
                                ParentIDMapping.ParentMapping.TableName,
                                ParentIDMapping.ColumnName,
                                null!,
                                "",
                                !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                !OnDeleteDoNothingValue && !DatabaseJoinsCascade);
            }
            foreach (var TempMapping in ForeignMapping)
            {
                foreach (var ForeignIDMapping in TempMapping.IDProperties)
                {
                    JoinTable.AddColumn<object>(ForeignIDMapping.ParentMapping.TableName + ForeignIDMapping.ColumnName,
                                    ForeignIDMapping.PropertyType.To(DbType.Int32),
                                    ForeignIDMapping.MaxLength,
                                    false,
                                    false,
                                    false,
                                    false,
                                    false,
                                    ForeignIDMapping.ParentMapping.TableName,
                                    ForeignIDMapping.ColumnName,
                                    null!,
                                    "",
                                    !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                    !OnDeleteDoNothingValue && DatabaseJoinsCascade,
                                    !OnDeleteDoNothingValue && !DatabaseJoinsCascade);
                }
            }
        }
public override void Setup ( IMappingSource mappings , ISource sourceSpec ) { ForeignMapping = mappings . GetChildMappings < DataType > ( ) . SelectMany ( x => mappings . GetParentMapping ( x . ObjectType ) ) . Where ( x => x . IDProperties . Count > 0 ) . Distinct ( ) . ToList ( ) ; if ( ForeignMapping is null ) { throw new ArgumentException ( $" Foreign key IDs could not be found for  { typeof ( ClassType ) . Name } . { Name } " ) ; } var ParentMappings = mappings . GetChildMappings ( ParentMapping . ObjectType ) . SelectMany ( x => mappings . GetParentMapping ( x . ObjectType ) ) . Distinct ( ) ; var ParentWithID = ParentMappings . FirstOrDefault ( x => x . IDProperties . Count > 0 ) ; if ( string . IsNullOrEmpty ( TableName ) ) { var Class1 = ParentWithID . ObjectType . Name ; var Class2 = ForeignMapping . OrderBy ( x => x . ObjectType . Name ) . FirstOrDefault ( ) ? . ObjectType . Name ?? string . Empty ; if ( string . CompareOrdinal ( Class1 , Class2 ) < 0 ) { SetTableName ( Class1 + " " + Class2 ) ; } else { SetTableName ( Class2 + " " + Class1 ) ; } } if ( sourceSpec . Tables . Any ( x => x . Name == TableName ) ) { return ; } var JoinTable = sourceSpec . AddTable ( TableName ?? string . Empty , ParentMapping . SchemaName ) ; JoinTable . AddColumn < long > ( " " , DbType . UInt64 , 0 , false , true , false , true , false ) ; var ParentIDMappings = ParentMappings . SelectMany ( x => x . IDProperties ) ; DatabaseJoinsCascade = ForeignMapping . Any ( TempMapping => ! ParentMappings . Contains ( TempMapping ) ) ; var Prefix = string . Empty ; if ( ForeignMapping . Any ( TempMapping => ParentWithID == TempMapping ) ) { Prefix = " " ; } foreach ( var ParentIDMapping in ParentIDMappings ) { JoinTable . AddColumn < object > ( Prefix + ParentIDMapping . ParentMapping . TableName + ParentIDMapping . ColumnName , ParentIDMapping . PropertyType . To ( DbType . Int32 ) , ParentIDMapping . MaxLength , false , false , false , false , false , ParentIDMapping . ParentMapping . TableName , ParentIDMapping . ColumnName , null ! , " " , ! OnDeleteDoNothingValue && DatabaseJoinsCascade , ! OnDeleteDoNothingValue && DatabaseJoinsCascade , ! OnDeleteDoNothingValue && ! DatabaseJoinsCascade ) ; } foreach ( var TempMapping in ForeignMapping ) { foreach ( var ForeignIDMapping in TempMapping . IDProperties ) { JoinTable . AddColumn < object > ( ForeignIDMapping . ParentMapping . TableName + ForeignIDMapping . ColumnName , ForeignIDMapping . PropertyType . To ( DbType . Int32 ) , ForeignIDMapping . MaxLength , false , false , false , false , false , ForeignIDMapping . ParentMapping . TableName , ForeignIDMapping . ColumnName , null ! , " " , ! OnDeleteDoNothingValue && DatabaseJoinsCascade , ! OnDeleteDoNothingValue && DatabaseJoinsCascade , ! OnDeleteDoNothingValue && ! DatabaseJoinsCascade ) ; } } }
Sets up the property (used internally)
Sets up the property ( used internally )
Foreign key IDs could not be found for {typeof(ClassType).Name}.{Name}
Foreign key IDs could not be found for { typeof ( ClassType ) . Name } . { Name }
mappings
IMappingSource
None
sourceSpec
ISource
The source spec.
The source spec .