None
None

Reputation: 5670

Query Single Item and convert to Model using Dapper

I am querying a single column from my database using Dapper. The Column content is a Json String. I want to desterilize it straightaway. And my code looks like this

string sql =@"SELECT [col1] FROM [table] where col3=@col3"
var data= await _connection.QuerySingleOrDefaultAsync<mymodelclass>(sql, parameters);

All looks good to me but this gives only empty object. My modelclass structure

public  class mymodelclass
{
    public string LevelTwo { get; set; }
    public string LevelThree { get; set; }
    public string LevelFour { get; set; }
    public string LevelFive { get; set; }
}
  

Col1 content

{
    "LevelTwo": "05 Planning, Budgeting and Forecasting",
    "LevelThree": "5A Planning, Budgeting and Forecasting",
    "LevelFour": "5A.07 Prepare Forecasts, Finalize Presentations / Reports",
    "LevelFive": "zxczx",
}

Upvotes: 2

Views: 1829

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064214

Dapper doesn't perform JSON deserialization, so: query the data as a string (via var json = connection.QuerySingle<string>(query, args); or similar), then run that string through your choice of JSON deserializer - JsonConvert.DeserializeObject<mymodelclass>(json) or similar.

Upvotes: 6

Related Questions