简体   繁体   中英

MVC AutoMapper models are different?

Will Automapper work, if I try to map a ModelMetadata that has only 5 properties and the Model.cs from the .edmx contains say 50 properties?

I basically created a ModelMetadata to customize the data annotations for five properties, but I was wondering if Automapper has problems mapping only 5 fields and not all 50 properties?

Will it just ignore the other 45 properties, if I only decided to update only 5 properties of a record to a database?

Automapper gives you auto property mapping ie if the property is the same on source and destination it will map. Others on the source and the destination that dont match will be ignored. You can also explicitly tell it how to map using ForMember eg

Mapper.CreateMap<CalendarEvent, CalendarEventForm>()
.ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))   
.ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.EventDate.Hour))   
.ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.EventDate.Minute));

The above example is pulled from the codeplex project site which fleshes this out properly here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM