Binding data from table (EF) to view model (MVC 5/C#) where field names are different

Let's say I have a table in my DB, tblBooks. The fields are:

Id Title AuthorId ISBN Copyright 

In my application I have a corresponding view model, BookViewModel, with the following properties:

BookId BookName AuthorId ISBN CopyrightDate 

Note that the field names and properties have different names even though one could easily map them.

Now I want to load the data from the table (via EF with my context dbContext) into a list:

 List<BookViewModel> books = new List<BookViewModel>(); 

I would of course like to do the following:

books = dbContext.tblBooks(); 

However, because the names don't match it won't work. Is there some type of attribute I can add to the field names in the view model class to tell it how to map the fields to the properties without having to loop through each record one by one?

Note: Let's assume the data types are correct and map without a problem.

My google-fu was not successful. Perhaps there is a term that I need to use? Most of the examples were of binding data from a form to a model for a single record.

Thanks,

by 0xZV2Rkm via /r/csharp

Leave a Reply