oceanicwang / dapper-dot-net

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

dapper 1.7 enum deserialization problem #72

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. public enum Role { Admin = 1, Authenticated = 2 };
2. var role = Current.DB.Query<Role>("SELECT 1 as Role").First();

What is the expected output? What do you see instead?
expected role will be Role.Admin but always return 0 instead

What version of the product are you using? On what operating system?
dapper-dot-net version 1.7 from nuget package using Windows 7 netfx 4

Please provide any additional information below.

Original issue reported on code.google.com by anton.he...@gmail.com on 23 Nov 2011 at 7:26

GoogleCodeExporter commented 8 years ago
forget to mention that the Db is MySql and dapper 1.6 return expected output

Original comment by anton.he...@gmail.com on 23 Nov 2011 at 7:32

GoogleCodeExporter commented 8 years ago
Update:

after few testing with console application and fresh mvc3 apps found that the 
behavior of dapper enum mapping inconsistent when map the enum type directly. 
mapping enum as property of class somehow consistently return correct map

   public enum Role
   {
       Anonymous = 0, Authenticate = 1, Administrator = 2
   }

   public class User
   {
       public int Id { get; set; }
       public Role Role { get; set; }
   }

   var user = Current.Db.Query<User>(@"SELECT roleId as Role, userId as Id 
      FROM users_roles
      WHERE userId=@id", new { id = 2 }).FirstOrDefault();

the result of user.Role is somehow return expected output

Original comment by anton.he...@gmail.com on 7 Dec 2011 at 6:21

GoogleCodeExporter commented 8 years ago
The problem still exists in 1.8 and the latest source code too. Attached is a 
patch file to the tests that adds an enum property to the multimap test to 
prove it is broken.

Original comment by drew.fre...@gmail.com on 19 Feb 2012 at 9:15

Attachments:

GoogleCodeExporter commented 8 years ago
I can reproduce this as well in 1.8 from NuGet (using SQL Server 2008 R2). For 
time being I just cast the value to the enum type:

var role = (Role) Current.DB.Query<int>("SELECT 1 as Role").First();

Original comment by kevin.e....@gmail.com on 19 Mar 2012 at 4:10

GoogleCodeExporter commented 8 years ago
This should now be fixed (recent code, not nuget)

Original comment by marc.gravell on 6 Aug 2014 at 3:31

GoogleCodeExporter commented 8 years ago
I can still reproduce it

    public enum Premium : sbyte
    {
        None = -1,
        Bronze = 1,
        Silver,
        Gold,
        Platin
    }

    public class Test
    {
        public Premium Premium { get; set; }
    }

    var result = Current.DB.Query<Test>("select * from test").First();

Note: the value for premium in database is -1 (type: smallint(2), signed). 
However the value in result will be Bronze (1).

Original comment by BoardT...@googlemail.com on 11 Apr 2015 at 1:25