brucezhang80 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

Index outside the bounds of the array exception when using MultiMapping #28

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
> What steps will reproduce the problem?

When I use Multi-Mapping, I get an index outside the bounds of the array 
exception in SqlMapper.cs line 892.  The code is the Add call here:

            var names = new List<string>();
            for (int i = startBound; i < startBound + length; i++)
            {
                names.Add(reader.GetName(i));      <-------- Exception
            }

This loop is executed twice.  The first finishes without error.  On the second, 
StartBound is 15 and length is 1.  However the reader at this point has just 
one row - this an index out of bounds exception.

My calling code is:

var insertMessages = mailDB.Connection.Query<MessageTransfer, AddressTransfer, 
MessageTransfer>("SELECT [messageId] ,[folderId] ,[subject] ,[date] ,[header] 
,[isRead] ,[isReplied] ,[isForwarded] ,CAST([format] as INT) as 
format,[charset] ,[contentSize] ,[content], emailAddress, friendlyName FROM 
Messages m LEFT JOIN Addresses a ON a.addressId = m.fromAddressId WHERE 
messageId IN @MessageIds", (m, a) => { m.FromAddress = a; return m; }, 
insertMessageIdsParameter);

The classes in question are:

    public partial class MessageTransfer : LiveObject 
    {
        public int MessageId { get; set; }
        public int FolderId { get; set; }
        public string Subject { get { return _subject; } set { _subject = value == null ? "" : value; } } private string _subject = "";
        public DateTime Date { get; set; }
        public string Header { get { return _header; } set { _header = value == null ? "" : value; } } private string _header = "";
        public bool IsRead { get; set; }
        public bool IsReplied { get; set; }
        public bool IsForwarded { get; set; }
        public ContentFormat Format { get; set; }
        public string Charset { get { return _charset; } set { _charset = value == null ? "" : value; } } private string _charset = "";
        public int ContentSize { get; set; }
        public string Content { get { return _content; } set { _content = value == null ? "" : value; } } private string _content = "";
        public override int LiveId { get { return MessageId; } }
...
}

and 

    public partial class AddressTransfer : TransferObject 
    {
        public string EmailAddress { get { return _emailAddress; } set { _emailAddress = value == null ? "" : value; } } private string _emailAddress = "";
        public string FriendlyName { get { return _friendlyName; } set { _friendlyName = value == null ? "" : value; } } private string _friendlyName = "";
...
}

The schema is:

CREATE TABLE [dbo].[Messages](
    [messageId] [int] IDENTITY(1,1) NOT NULL,
    [folderId] [int] NOT NULL,
    [subject] [nvarchar](max) NOT NULL,
    [fromAddressId] [int] NOT NULL,
    [date] [smalldatetime] NOT NULL,
    [header] [nvarchar](max) NOT NULL,
    [isRead] [bit] NOT NULL,
    [isReplied] [bit] NOT NULL,
    [isForwarded] [bit] NOT NULL,
    [format] [int] NOT NULL,
    [charset] [varchar](64) NOT NULL,
    [contentSize] [int] NOT NULL,
    [content] [nvarchar](max) NOT NULL
)

and

CREATE TABLE [dbo].[Addresses](
    [addressId] [int] IDENTITY(1,1) NOT NULL,
    [emailAddress] [nvarchar](256) NOT NULL,
    [friendlyName] [nvarchar](256) NOT NULL
)

Please let me know if I can provide any other information.

Original issue reported on code.google.com by pet...@gmail.com on 29 May 2011 at 7:07

GoogleCodeExporter commented 9 years ago
OK - stupid user error.  This is fixed if I set the "splitOn" parameter to 
EmailAddress.  It may help to add a comment on that line that says "Index out 
of range here? Try setting splitOn".

Original comment by pet...@gmail.com on 29 May 2011 at 7:30

GoogleCodeExporter commented 9 years ago
Peter, you should be splitting on messageId as far as I can see, agree about 
improving the error 

Original comment by sam.saff...@gmail.com on 29 May 2011 at 11:55

GoogleCodeExporter commented 9 years ago
Yes, exactly - I had neglected to properly set the "split on".  So I change my 
bug request to one of making the error less opaque (maybe with a comment).  
Many thanks for Dapper!

Original comment by pet...@gmail.com on 30 May 2011 at 12:32

GoogleCodeExporter commented 9 years ago
Improved the error message today ... 

Original comment by sam.saff...@gmail.com on 13 Jul 2011 at 7:21