0

I'm not sure if I'm being clear in the title but I'd like to "load" information from a SQL Server database into a list of objects. I'm new to c# and honestly haven't done any coding in a while.

Essentially the table would have columns: app_name, app_type, app_disposition and the object has properties: name, type, disposition. I've got what I want working using Dapper and simply making the object properties the same as the table columns.

Just curious if you could load but using different object property names.

CodeMoto
  • 331
  • 1
  • 6
  • 24

2 Answers2

2

With Dapper, simplest solution is to use aliases.

Your class is:

public class MyPoco
{
    public string Name {get;set;}
    //Declare other properties here
}

And, you fill this class as below:

string sql = "SELECT app_name as Name, [include other columns here]
              FROM MyTable";

using (var conn = GetOpenConnection())
{
    var myPocoList = conn.Query<MyPoco>(sql);
}

GetOpenConnection method above simply returns open connection depending on your RDBMS.

Please note that there are many other ways to map the miss-matching column and property names. Please refer this Q&A for more details.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
1

Yes, you can, and you have to use the "Custom Mapping" feature. Here's a detailed article I wrote on the subject, along with code samples, to shows how you can do it.

https://medium.com/dapper-net/custom-columns-mapping-1cd45dfd51d6

Hint: Use Dapper.Fluent-Map plugin

mauridb
  • 1,467
  • 9
  • 12