1

I've two POCO-s. A and B.

public class A{
    [Slapper.AutoMapper.Id]
    public int Id { get; set; }
    public B BType { get; set; }
    // Rest of the fields
}

public class B{
    [Slapper.AutoMapper.Id]
    public int Id { get; set; }
    public string Name {get;set;}
}

T-SQL Stored Procedure result:

ID      B_Id    B_Name
1       1       B1
2       2       B2

My code to get List with Dapper is:

List<A> aList = new List<A>();
            using (IDbConnection connection = new SqlConnection(GlobalConfig.ConnectionString()))
            {
                var p = new DynamicParameters();
                p.Add("@Id", someId);
                // Here debug shows me 2 correct objects inside of var list
                var list = connection.Query<dynamic>("[spApp_getBlaBlaByID]", p, commandType: CommandType.StoredProcedure);

                // After casting I got only 1
                aList =(Slapper.AutoMapper.MapDynamic<A>(list) as IEnumerable<A>).ToList();

            }
            return aList; // Debugging shows only 1 one of the objects

So, what is wrong with my code? Please help to find mistake. P.S. I came to C# from Java. Maybe in C# world Slapper.Automapper is not in trend anymore. What is flexible and modern solution to map POCO-s with DAPPER?

SNabi
  • 69
  • 1
  • 5
  • Are you sure the method is supposed to return 2 rows? `spApp_getBlaBlaByID` should, logically, retrieve a single row - containing the entity with ID provided as a parameter. – orhtej2 Mar 13 '18 at 21:42
  • I ommited SP name, real name is getParentsByStudentID – SNabi Mar 14 '18 at 13:10

1 Answers1

1

To begin with, I do not understand need of Slapper here. Dapper is able to handle this role. So code something like below should work without Slapper.

List<A> list = connection.Query<A>("[spApp_getBlaBlaByID]"......

Second is what mentioned by @orhtej2 in comments. Name of stored procedure ends with "ByID". This convention suggests that it will return single record. Are you sure SP returns multiple records? If yes, above code sample should do. If it returns single record, change above to something like below:

A a = connection.Query<A>("[spApp_getBlaBlaByID]"......
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141