Esempio n. 1
0
        public override BulkCopyRowsCopied BulkCopy <T>(DataConnection dataConnection, BulkCopyOptions options, IEnumerable <T> source)
        {
            if (_bulkCopy == null)
            {
                _bulkCopy = new OracleBulkCopy(this, GetConnectionType());
            }

            IList <T> sourceList = null;

            if (options.RetrieveSequence)
            {
                var entityDescriptor = dataConnection.MappingSchema.GetEntityDescriptor(typeof(T));

                bool foundIdentityColumn = false;
                foreach (ColumnDescriptor column in entityDescriptor.Columns)
                {
                    foundIdentityColumn = foundIdentityColumn || column.IsIdentity;

                    var sequenceName = column.MemberInfo
                                       .GetCustomAttributesEx(typeof(SequenceNameAttribute), true)
                                       .OfType <SequenceNameAttribute>()
                                       .Select(a => a.SequenceName)
                                       .FirstOrDefault(s => !string.IsNullOrEmpty(s));

                    if (!string.IsNullOrWhiteSpace(sequenceName))
                    {
                        sourceList = sourceList ?? source.ToList();
                        var sequences = ReserveSequenceValues(dataConnection, sourceList.Count, sequenceName);

                        for (var i = 0; i < sourceList.Count; i++)
                        {
                            var item  = sourceList[i];
                            var value = Converter.ChangeType(sequences[i], column.MemberType);
                            column.MemberAccessor.SetValue(item, value);
                        }
                    }
                }

                options.KeepIdentity = foundIdentityColumn;
            }

            return(_bulkCopy.BulkCopy(
                       options.BulkCopyType == BulkCopyType.Default ? OracleTools.DefaultBulkCopyType : options.BulkCopyType,
                       dataConnection,
                       options,
                       sourceList ?? source));
        }