3

I have a simple problem which I think must have a simple answer... well maybe. :)

I'm writing a MVC3 application. I have an existing database from the previous version of the application, so it's a data driven app. Instead of using EF to access the data and LINQ expressions, I'd prefer to use stored procedures / SQL commands.

What I want to do is run a SQL command, which returns a datatable object and then map this to a model which contains exactly the same fields. Sadly, I'm doing this by hand right now, iterating through the data rows. There must be some better approach?

Thanks for any tips... and for not sniggering at my newbie question.

philbird
  • 789
  • 1
  • 8
  • 15
  • not sure if autoMapper can help you, have a look here: http://automapper.org/ – Davide Piras Sep 24 '11 at 10:54
  • 1
    I'm going to give that a shot. It looks like the man for the job... although, I think you need to setup the mappings explicitly, whereas the Datatable needs to be an implicit relationship... or something like that. – philbird Sep 24 '11 at 11:01
  • 1
    Do you have to use the DataTable? What about something like dapper? (http://code.google.com/p/dapper-dot-net/) – Jon Sep 24 '11 at 11:03

1 Answers1

2

AutoMapper should do the job, check it out here: AutoMapper

there are also plenty of questions and answers about it here on SO:

AutoMapper: Mapping between a IDataReader and DTO object

Automapper Mapping

AutoMapper issue

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147