automapper

AutoMapper ForMember Ignore not working

强颜欢笑 提交于 2019-12-05 08:16:14
Doing a copy of the same entity type in an MVC app, but looking to ignore copying the primary key (doing an update to an existing entity). But setting the Id column to ignore in the map below is not working and the Id is being overwritten. cfg.CreateMap<VendorContact, VendorContact>() .ForMember(dest => dest.Id, option => option.Ignore()) .ForMember(dest => dest.CreatedById, option => option.Ignore()) .ForMember(dest => dest.CreatedOn, option => option.Ignore()) ; Executing the Map: existingStratusVendorContact = Mapper.Map<VendorContact>(vendorContact); Saw this other answer , but it appears

AutoMapper and “UseDestinationValue”

帅比萌擦擦* 提交于 2019-12-05 08:04:52
What does UseDestinationValue do? I am asking because I have a base and inherited class, and for the base class, I would love to have AutoMapper take existing values for me. Will it do that? (I have looked and the only examples I can see for UseDestinationValue involve lists. Is it only for lists? could I do this: PersonContract personContract = new PersonContract {Name = 'Dan'}; Person person = new Person {Name = "Bob"}; Mapper.CreateMap<PersonContract, Person>() .ForMember(x=>x.Name, opt=>opt.UseDestinationValue()); person = Mapper.Map<PersonContract, Person>(personContract); Console

How do you ignore/persist values in MVC when your view-model doesn't have as many fields as your domain model?

五迷三道 提交于 2019-12-05 07:43:05
I have a site where I'm using fluentNhibernate and Asp.net MVC. I have an Edit view that allows user to edit 8 of the 10 properties for that record (object). When you submit the form and the Model binds, the two un-editable fields come back in the view-model as Empty strings or as default DateTime values depending on the type of property. Because I'm also using AutoMapper to map my view-model to my Domain Entity, I cannot just load a fresh copy of my object from the database and manually set the 2 missing properties. Whats the best way to persist those fields that I don't want edited? One way

Automapper 5.0 global configuration

筅森魡賤 提交于 2019-12-05 06:54:57
问题 I am using the code below in AutoMapperConfig.cs in App_Start folder. I initialized it in Global.asax as AutoMapperConfiguration.Configure() But I am unable to use the Mapper.Map<Hospital, MongoHospital> in my controller. It is throwing an exception that no mappings are defined. It was working in previous versions of Automapper which were supporting Mapper.CreateMap<> methods. I am confused how to use MapperConfiguration instance. public static class AutoMapperConfiguration { public static

When should I be using AutoMapper and when not

旧城冷巷雨未停 提交于 2019-12-05 06:03:33
I have a Data layer which holds my EF6 DbFirst edmx, repositories, and AutoMappings. I also have a Model layer with a Poco for each auto generated entity in my Data layer. The properties pretty much match exactly except for a few name changes. AutoMapper is installed to my DataLayer only and this is where I set all of my mappings in a config file. At this point I have a mapping from each DataLayer entity to each ModelLayer entity and each ModelLayer entity to each DataLayer entity. Any name changes are specified in the mappings. Since it is setup this way in my repository save methods the

Projection using contextual values in AutoMapper

半城伤御伤魂 提交于 2019-12-05 05:20:01
I'm currently evaluation whether AutoMapper can be of benefit to our project. I'm working on a RESTful Web API using ASP.NET Web API, and one of the things I must return is a resource that contains links. Consider this simplified example, using the following domain object: public class Customer { public string Name { get; set; } } I need to map this into a resource object, sort of like a DTO but with added properties to facilitate REST. This is what my resource object may look like: public class CustomerResource { public string Name { get; set; } public Dictionary<string, string> Links { get;

How to map a string to a date in automapper?

家住魔仙堡 提交于 2019-12-05 04:45:39
I have a string that is a valid date but it is a string and it needs to be a string. However when I try to auto map it to a datetime it throws an exception Trying to map System.String to System.DateTime. Trying to map System.String to System.DateTime. Using mapping configuration for ViewModels.FormViewModel to Framework.Domain.Test Destination property: DueDate Missing type map configuration or unsupported mapping. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. Description: An unhandled exception occurred during the execution of the current web request. Please review the

How do I get AutoMapper to map this?

馋奶兔 提交于 2019-12-05 04:14:20
问题 Say I have this class: public class Account { public int AccountID { get; set; } public Enterprise Enterprise { get; set; } public List<User> UserList { get; set; } } When I use AutoMapper to map the Account class, I would also like it to map the Enterprise class, and the list of users (UserList) in the returned object. How can I get AutoMapper to do this? Thanks! 回答1: AutoMapper does that out of-the-box if you provide a configuration for the Enterprise and User type. Configuration looks like

How to configure Automapper to be injected with Ninject 2.0?

寵の児 提交于 2019-12-05 02:56:57
问题 There are configuration examples for Structure Map and Windsor: http://www.cprieto.com/index.php/2009/08/20/using-automapper-with-castle-windsor/ But I haven't found anything for Ninject. Do you know how to translate those mappings to Ninject? 回答1: public class AutoMapperModule : NinjectModule { public override void Load() { Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine); } } 回答2: It's really very easy, just load this module: public class AutoMapperModule : NinjectModule { public

Automapper: Map to Protected Property

喜你入骨 提交于 2019-12-05 02:48:05
I need to map to a protected property on a class using Automapper . I've got a public method exposed on this class that is used to set values to the property. This method requires a parameter . How can I map a value to this class? Destination Class: public class Policy { private Billing _billing; protected Billing Billing { get { return _billing; } set { _billing = value; } } public void SetBilling(Billing billing) { if (billing != null) { Billing = billing; } else { throw new NullReferenceException("Billing can't be null"); } } } Here's what my Automapper code (pseudo code) looks like: Mapper