Exemplo n.º 1
0
        /// <summary>
        /// Sets up the property (used internally)
        /// </summary>
        /// <param name="mappings"></param>
        public override void Setup(IMappingSource mappings)
        {
            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();

            SetNullOnDelete = !OnDeleteDoNothingValue && ForeignMapping.Any(x => !ParentMappings.Contains(x));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the column information.
        /// </summary>
        /// <param name="mappings">The mappings.</param>
        public override void SetColumnInfo(IMappingSource mappings)
        {
            var Prefix           = string.Empty;
            var ParentMappings   = mappings.GetChildMappings(ParentMapping.ObjectType).SelectMany(x => mappings.GetParentMapping(x.ObjectType)).Distinct();
            var ParentIDMappings = ParentMappings.SelectMany(x => x.IDProperties);
            var ParentWithID     = ParentMappings.FirstOrDefault(x => x.IDProperties.Count > 0);

            if (ForeignMapping.Any(TempMapping => ParentWithID == TempMapping))
            {
                Prefix = "Parent_";
            }

            var TempColumns = new List <IQueryColumnInfo>();

            TempColumns.AddRange(ParentIDMappings.ForEach(x =>
            {
                return(new ComplexColumnInfo <ClassType, ClassType>(
                           x.GetColumnInfo()[0],
                           Prefix + x.ParentMapping.TableName + x.ColumnName,
                           y => y,
                           false,
                           ParentMapping.SchemaName,
                           TableName ?? string.Empty
                           ));
            }));
            TempColumns.AddRange(ForeignMapping.SelectMany(TempMapping =>
            {
                return(TempMapping.IDProperties.ForEach(x =>
                {
                    return new ComplexListColumnInfo <ClassType, DataType>(
                        x.GetColumnInfo()[0],
                        x.ParentMapping.TableName + x.ColumnName,
                        CompiledExpression,
                        true,
                        ParentMapping.SchemaName,
                        TableName ?? string.Empty
                        );
                }));
            }));
            Columns = TempColumns.ToArray();
        }
Exemplo n.º 3
0
        /// <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>
        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);
                }
            }
        }