automapper

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

与世无争的帅哥 提交于 2019-12-07 03:52:29
问题 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

AutoMapper ForMember Ignore not working

孤者浪人 提交于 2019-12-07 03:34:01
问题 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:

automapper, mapping to an interface

冷暖自知 提交于 2019-12-07 03:32:18
问题 I am using automapper (for .net 3.5). Here is an example to illustrate what I am trying to do: I want to map an A object to a B object. Class definitions: class A { public I1 MyI { get; set; } } class B { public I2 MyI { get; set; } } interface I1 { string StringProp1 { get; } } interface I2 { string StringProp1 { get; } } class CA : I1 { public string StringProp1 { get { return "CA String"; } } public string StringProp2 { get; set; } } class CB : I2 { public string StringProp1 { get { return

Automapper map from one object to nested objects

空扰寡人 提交于 2019-12-07 03:28:48
问题 What is the best way to map inner objects with Automapper 2.0 Use the solution in this question (Automapper 1.0) Create a Custom Value Resolvers ? public class DTOObject { // MainObject public int Id { get; set; } public string Name { get; set; } // SubObject (TopObject) public string TopText { get; set; } public string TopFont { get; set; } // SubObject (BottomObject) public string BottomText { get; set; } public string BottomFont { get; set; } } public class MainObject { public int Id { get

Can Automapper ignore void methods?

妖精的绣舞 提交于 2019-12-07 02:53:22
问题 I am new to Automapper, so I am not sure if this is possible. I would like to map a class, but get it to ignore methods that are void. Below is an illustration of the code I have. When I run this I get the following exception message. An unhandled exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll Unfortunately it isn't an option to change the interface, so I assume if this is possible there is some sort of configuration I am missing? public interface IThing

Projection using contextual values in AutoMapper

折月煮酒 提交于 2019-12-07 01:47:09
问题 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

How to map a string to a date in automapper?

这一生的挚爱 提交于 2019-12-07 01:27:40
问题 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.

When should I be using AutoMapper and when not

时光怂恿深爱的人放手 提交于 2019-12-07 00:00:51
问题 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

Automapper: Map to Protected Property

三世轮回 提交于 2019-12-06 23:10:07
问题 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

Entity core Mapping entity types with join table

强颜欢笑 提交于 2019-12-06 23:07:25
I've started using entity core and I ran into one problem. I'm using automapper in my project and don't know how to populate my join table(BookAuthor). In MVC I worked with ICollections but entity core does not yet support working with many to many relationship entities and now I have to work with join table. Question: How can i map my Bookviewmodel to the Book? I don't know what should I do with my BookAuthors list. Maybe I shouldn't populate it? I tried something like that but it's not working. CreateMap<BookViewModel, Book>() .ForMember(b => b.BookAuthors, opt => opt.MapFrom(b => b.Authors