Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dapper do an auto-select of columns that match properties?

Tags:

orm

dapper

When using Dapper-dot-net, if your querying to a strongly typed results, and your SQL just has a:

select * 

Will Dapper automappically only do a select on the columns that match the fields in your object? I think PetaPOCO does this but I ran into some problems with dapper that I thought were attributed to this mismatch.

Example,

conn.Query<article>("select * from Article");

Will this work if the Article table contains other columns that are extraneous to the article object?

like image 597
Jay Stevens Avatar asked May 16 '11 11:05

Jay Stevens


People also ask

How do you pass multiple parameters in dapper query?

To use the dynamic parameters you have to set each parameter in the stored procedure, so you will have to loop through the list and add each property to the coresponding parameter.

How does Dapper mapping work?

Dapper maps data to the first type in the same way as it does if only one generic parameter has been supplied to the QueryAsync<T> method. If is then told to map data to the Category type, and to assign the resulting object to the product's Category property.

What is splitOn in dapper?

splitOn: CustomerId will result in a null customer name. If you specify CustomerId,CustomerName as split points, dapper assumes you are trying to split up the result set into 3 objects. First starts at the beginning, second starts at CustomerId , third at CustomerName .


1 Answers

Yes it does - I tried this over the weekend, even with a query involved two tables joined by a FK constraint. I created two classes that represented only parts of those underlying tables, and those properties present will be filled just fine, anything that's not in the classes will be ignored. Works like a charm!

On the other hand: if you only need a few column - you should really specify those explicitly in your SQL query - as a general best practice! No point in selecting everything, if you need only a handful of columns....

like image 136
marc_s Avatar answered Oct 01 '22 10:10

marc_s