yuan8825399 / dapper-dot-net

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

Dapper fails to update datetime with correct value if more than one constructor is present in the class #121

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
using class:
    using System;
    public class DapperTest
    {
        public DapperTest(string hello, DateTime world)
        {
            this._hello = hello;
            this._worldA = world;
        }
        public DapperTest() { }

        private string _hello;
        public string Hello
        {
            get { return _hello; }
            set { _hello = value; }
        }

        private DateTime _worldA = new DateTime();
        public DateTime WorldA
        {
            get { return _worldA; }
            set { _worldA = value; }
        }
    }

with table

CREATE TABLE [dbo].[DapperTest](
    [hello] [varchar](50) NOT NULL,
    [world] [datetime] NULL
) ON [PRIMARY]

and data

INSERT INTO [mercury_db].[dbo].[DapperTest]
           ([hello]
           ,[world])
     VALUES
           ('hello', '3/23/1978')

then run the following code...

List<DapperTest> dt = (List<DapperTest>)cn.Query<DapperTest>("select hello, 
world from dbo.DapperTest");

you would expect to get an object with "hello" and "3/23/2012 01:01:10 PM" in 
it.  what you actually get is an object with "hello" and "1/1/0001 12:00:00 AM".

as near as i can tell, Dapper isn't actually calling the constructor with the 
defined string,DateTime, but is instead calling the default constructor.  

So that feels like bug #1 (that Dapper doesn't call the proper constructor)

What makes it especially confusing is that Dapper, when it calls the default 
constructor, it manages to populate either the private string _hello or the 
public string Hello, even though they both differ from the table column name. 
Which is confusing. I suppose its guessing.  But that feels like a bug also.

What version of the product are you using? On what operating system?
Currently using revision: e8ead8d82e55 of the SqlMapper class.  I'm running 
SqlServer 2008, on Windows 7 (.Net 4.0)

Thanks!

Original issue reported on code.google.com by walljm on 9 Nov 2012 at 12:38

GoogleCodeExporter commented 9 years ago
I believe this is by design. If you need to call the constructor you can use 
the method specified here: 
http://stackoverflow.com/questions/8994933/call-custom-constructor-with-dapper

Otherwise you would need to update your select statement to this:

"select hello, world AS worldA from dbo.DapperTest" so that the property names 
match.

Original comment by ppatters...@gmail.com on 9 Nov 2012 at 4:53