简体   繁体   English

MVC AutoMapper型号不同吗?

[英]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? 如果我尝试映射仅具有5个属性的ModelMetadata,而.edmx中的Model.cs包含50个属性,那么Automapper是否可以工作?

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? 我基本上创建了一个ModelMetadata来自定义五个属性的数据注释,但是我想知道Automapper是否仅映射5个字段而不是映射全部50个属性有问题?

Will it just ignore the other 45 properties, if I only decided to update only 5 properties of a record to a database? 如果我仅决定将记录的仅5个属性更新到数据库,它将忽略其他45个属性吗?

Automapper gives you auto property mapping ie if the property is the same on source and destination it will map. Automapper为您提供自动属性映射,即,如果属性在源和目标上相同,它将进行映射。 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 您还可以明确地告诉它如何使用ForMember进行映射,例如

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 上面的示例是从Codeplex项目站点提取的,在这里可以对其进行适当充实

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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