Conversion of entity types? AutoMapper OR Custom

Source: Internet
Author: User

After introducing the Entity Framework, the system has a new requirement, that is, the conversion between ViewModel and model.

There are various realms between this. Take it out today and taste it.


1 with AutoMapper

The method is simple. ① Adding a reference to AutoMapper


② creates a mapping relationship between two entities at the time of reference. All we have to do is tell AutoMapper (call the static method of the Mapper class Createmap and pass in the type to be mapped) to the two types that will be mapped:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >    Mapper.createmap<studentviewmodel, basicstudententity> ();</span>

③ Implementation Conversion

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > basicstudententity enstudent = Mapper.map<studentviewmodel, basicstudententity> (student);</span>
If there is a null attribute in Studentviewmodel, AutoMapper will also set the corresponding property in basicstudententity to null when mapping. Of course, what I'm talking about is one of the simplest ways to use it. The simplest, but its usable range is also minimal. The structure is fully consistent and the field names are exactly the same. In many cases, some of the fields in ViewModel are not exactly the same as the model. Lead us to use such a way, very inflexible.


In fact, AutoMapper also has a use case, that is, field names do not have to be exactly the same. (But the meaning should be the same), so we can define simple mapping rules between types.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >1.  var map = mapper.createmap<studentviewmodel,basicstudententity> (); 2. Map. Formember (d = d.name, opt = = opt.  Mapfrom (s = s.schoolname)); </span>

But even so, it doesn't solve my other problem. That is, when the Studentviewmodel is more than the field in the basicstudententity (meaning is inconsistent), it cannot be converted.


2 Customizing

This is when we think of our own custom method of converting.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > #region 3.0.0 Student Management Public methods convert view to model (basicstudent)///<summary>//Students manage public methods convert view to mode L (basicstudent)///</summary>//<param name= "enstudentlist" ></param>//<ret Urns></returns> public list<basicstudententity> Changeviewtobasicstudentmodel (List< Studentviewmodel> enstudentlist) {list<basicstudententity> studentlist = new List<basicstud            Ententity> ();                foreach (var item in enstudentlist) {basicstudententity student = new Basicstudententity () {StudentID = Item. StudentID, Studentno = Item. Studentno, Usercode = Item. Usercode, Entrypartytime = Item. Entrypartytime, speciality = Item. Speciality, healthcondition = Item.   Healthcondition,                 Examineenumber = Item. Examineenumber, Fathername = Item. Fathername, Mothername = Item. Mothername, Fatherphone = Item. Fatherphone, Motherphone = Item. Mothername, Traindestination = Item. Traindestination, Note = Item. Note, Operator = Item. Operator, TimeStamp = Item. TimeStamp, Creditcardno = Item. Creditcardno, Name = Item. Name, Sex = Item. Sex, Politicalstatus = Item. Politicalstatus, Previousname = Item. Previousname, Email = Item. Email, CellPhoneNumber = Item. CellPhoneNumber, Hometelephone = Item. Hometelephone, birthplace = Item. Birthplace, HomeAddress = Item. HomeAddress, Nation = Item. Nation, Roomid = Item. Roomid, Directionid = Item. Directionid, ClassID = Item.                ClassID};            Studentlist.add (student);        } return studentlist; } #endregion </span>


Your own method is still handy. If it is the underlying package, it is not added to the generic type can be called by everyone together. This part does not own practice. for communication only.



Conversion of entity types? AutoMapper OR Custom

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.