AutoMapper specifying column names for mapping

Source: Internet
Author: User

With AutoMapper, there is no need to manually assign a one-to-a-idatareader to an entity field. This blog post is a record of the actual case.

The entity type is defined as follows:

 Public class uploadimage{    publicintgetset;}      Public string Get Set ; }}

The SRC attribute corresponds to the field name ImageUrl in the database.

Mapping requirements: from IDataReader to Ienumerable<uploadimage>, the code is as follows:

using(varconn =NewSqlConnection (Connectionstrings.default)) {    using(varCommand =Conn. CreateCommand ()) {Command.commandtype=System.Data.CommandType.Text; Command.commandtext="..."; awaitConn.        OpenAsync (); using(IDataReader reader =awaitcommand. Executereaderasync ()) {returnAutomapper.mapper.map<idatareader, ienumerable<uploadimage>>(reader); }    }}

This mapping can be implemented by configuring AutoMapper with the Imappingexpression.formember () method, with the following code:

This is a very representative
Automapper.mapper.createmap<idatareader, uploadimage>() = dest. SRC, Options and options. Mapfrom ( = = src. GetString (src. GetOrdinal ("ImageUrl"

AutoMapper specifying column names for mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.