Karan Desai
Karan Desai

Reputation: 3142

How to get started with using Dapper.Mapper?

I am using Dapper.Mapper for mapping Sql queries to my application classes directly. I believed Dapper.Mapper is just a syntactical sugar-coating over Dapper, the original ORM. But, I am still not able to head-start with it due to very less demos and examples. However I have tried this as per their git page:

var sql="Select * from Students where Grades>90"'
var conn=new MySqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
var studentRecord=conn.Query<Students, TopStudents>(sql);

And my POCO models are:

//Db table    
public class Students
{
    public int Id{get;set;}
    public string Name{get;set;}
    public float Grades{get;set;}
}

public class TopStudents
{
    public int Id{get;set;}
    public string Name{get;set;}
    public string Grades{get;set;}
    //...Few more properties
}

But I am getting following exception, No Writable Property for TopStudents found in types Students

Am I missing something? Please share any quick demo or tutorial. There is not even a proper documentation where I can find the exception's cause and resolve it myself. How to get started with Dapper.Mapper?

Upvotes: 0

Views: 4202

Answers (1)

markpsmith
markpsmith

Reputation: 4918

See the docs here, the example query uses select * and uses an inner join, so it's returning Employee AND Department objects. If it was only returning Employee objects, then the code would look like this:

var sql = @"select * from Employee";

var employee = connection.Query<Employee, Department>(sql);

Applying this to your code:

var sql="Select * from Students where Grades>90"'
var conn=new MySqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
var studentRecord=conn.Query<Students>(sql);

If you want to return Students and TopStudents, then you would change it to something like:

var sql="Select * from Students s
INNER JOIN TopStudents ts where s.Id = ts.Id AND s.Grades>90"'
var conn=new MySqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
var studentRecord=conn.Query<Students, TopStudents>(sql);

Upvotes: 1

Related Questions