4

I have a table :

create table scheduled_task
(
    scheduled_task_id serial primary key,
    description varchar(255),
    predicate varchar(10485760),
    path varchar(255),
    scheduled_task_type_id int references scheduled_task_type (scheduled_task_type_id)
);

My data object :

[Table("scheduled_task")]
public class DOScheduledTask
{
    [Key]
    [Column("scheduled_task_id")]
    public long? ScheduledTaskId { get; set; }

    [Column("description")]
    public string Description { get; set;}

    [Column("predicate")] 
    public string Predicate { get; set; }

    [Column("path")]
    public string Path { get; set; }

    [Column("scheduled_task_type_id")]
    public long? ScheduledTaskTypeId { get; set; }
}

I inserted one record into the table, that populates all fields.

I perform this in code :

var schedules = conn.Connection.Query<DOScheduledTask>("select * from scheduled_task ;");
var schedulesb = conn.Connection.GetList<DOScheduledTask>();

The first line yields a record, with everything but scheduled_task_id and scheduled_task_type_id are both null. For the second query, it is populated in full.

What am I doing wrong?

Liam
  • 27,717
  • 28
  • 128
  • 190
WynDiesel
  • 1,104
  • 7
  • 38
  • As you have probably noticed, the two missing columns are the ones where the table column name is different from the property name - it seems that [Dapper does not yet support mapping via `Column` attributes](https://github.com/StackExchange/Dapper/issues/722). Not sure what the _best_ alternative is, but I would suggest trying [this](https://stackoverflow.com/a/8904096/43846) first. – stuartd Nov 19 '19 at 11:32
  • @stuartd, thanks for your comment, but it does support it. I've done this in other projects, and it worked just fine. You will also note that, as mentioned in my answer, that the mapping does work when I don't specify a query string, but it doesn't work when I use the "Query" function from dapper. They both use the same mapping (and Column attrib). – WynDiesel Nov 19 '19 at 11:45
  • Did you use Dapper.Extensions and/or Dapper.Contrib in the other projects? – stuartd Nov 19 '19 at 11:47
  • 1
    @stuartd, yes. I do. I added it to the non-working project as well now, and no change. You led me to see something else now, however. The GetList() function is from Dapper.SimplerCRUD, and the Query() function is from vanilla Dapper. Clearly, different implementations on the mapping then. – WynDiesel Nov 19 '19 at 11:52
  • Can you use a [CustomPropertyTypeMap](https://stackoverflow.com/questions/26622063/using-custompropertytypemap-to-map-specific-properties)? – stuartd Nov 19 '19 at 12:10
  • @stuartd, exactly that. I had to create a mapper for vanilla Dapper, and now it works. Problem is that SimpleCRUD wants to user the column attribs, so now I have to cater for both. Not ideal, but it works. If you want to post this as an answer, Ill accept it. Thanks for your help! – WynDiesel Nov 19 '19 at 12:17
  • 2
    Glad you got it working. You're allowed to answer your own question on SO (and accept it!) which seems more appropriate here. – stuartd Nov 19 '19 at 12:42

1 Answers1

1

The problem is that the .Query and .GetList are from two different implementations; the one is from Dapper, and the other is from SimpleCRUD.

The solution was to create a custom mapper, due to the naming of the columns being different in code than in the DB.

Liam
  • 27,717
  • 28
  • 128
  • 190
WynDiesel
  • 1,104
  • 7
  • 38
  • why don;t you just use the [Contrib library](https://github.com/StackExchange/Dapper/tree/master/Dapper.Contrib)? – Liam Nov 22 '19 at 13:44