automapper

Automapper many to many mapping

左心房为你撑大大i 提交于 2019-12-04 20:48:53
问题 Patrick, thanks for advice about correct question! EDIT 1: I have three table for many to many relationship. Like this: GoodEntity: public partial class GoodEntity { public GoodEntity() { this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>(); } public int id { get; set; } public string name { get; set; } public string description { get; set; } public decimal cost { get; set; } public Nullable<decimal> price { get; set; } public virtual ICollection<GoodAndProviderEntity>

How to map from a flat entity to more than one DTO?

坚强是说给别人听的谎言 提交于 2019-12-04 20:10:25
I'm wishing to use AutoMapper in C# .NET Core to map from an already flattened entity to a nested set of DTO's. Also the DTO’s have a one to many relationship, which the flattened entity is hiding in structure. For example: public class Product { public int Id { get; set; } public string Name { get; set; } public int Price { get; set; } public int Weight { get; set; } } public class ProductDto { public string Name { get; set; } public IEnumerable<PriceDto> Prices { get; set; } } public class PriceDto { public int Price { get; set; } public int Weight { get; set; } } I'm aware of the ReverseMap

How do I debug AutoMapper.AutoMapperMappingException

北城余情 提交于 2019-12-04 19:46:41
问题 Is there some way to get some more detail from automapper when I get this exception: AutoMapper.AutoMapperMappingException Often it will tell me the 2 types of the mapping, but not which resolver or part of the mapping is failing. 回答1: Simple answer, is to call this method, preferably in your unit test. // ensure your configuration mappings are loaded first (bootstrapper) Mapper.AssertConfigurationIsValid(); see: http://docs.automapper.org/en/stable/Getting-started.html#how-do-i-test-my

Mapping Child Collections using AutoMapper

左心房为你撑大大i 提交于 2019-12-04 18:19:40
I am using Automapper for making a copy of an object My domain can be reduced into this following example Consider I have a Store with a collection of Location public class Store { public string Name { get; set;} public Person Owner {get;set;} public IList<Location> Locations { get; set;} } Below is an example of a store instance var source = new Store { Name = "Worst Buy", Owner = new Person { Name= "someone", OtherDetails= "someone" }, Locations = new List<Location> { new Location { Id = 1, Address ="abc" }, new Location { Id = 2, Address ="abc" } } }; My Mappings are configured as var

Automapper and request specific resources

痴心易碎 提交于 2019-12-04 18:19:31
I'm considering automapper for an asp mvc intranet app I am writing. My controllers are currently created using Unity dependency injection, where each container gets dependencies unique to the request. I need to know if automapper can be made to use a request specific resource ICountryRepository to look up an object, like so.... domainObject.Country = CountryRepository.Load(viewModelObject.CountryCode); Couple of options here. One is to do a custom resolver: .ForMember(dest => dest.Country, opt => opt.ResolveUsing<CountryCodeResolver>()) Then your resolver would be (assuming CountryCode is a

Using AutoMapper to map a string to an enum

早过忘川 提交于 2019-12-04 16:58:10
问题 I have the following classes domain and Dto classes: public class Profile { public string Name { get; set; } public string SchoolGrade { get; set; } } public class ProfileDTO { public string Name { get; set; } public SchoolGradeDTO SchoolGrade { get; set; } } public enum SchoolGradeDTO { [Display(Name = "Level One"] LevelOne, [Display(Name = "Level Two"] LevelTwo, } I used the following method: Mapper.CreateMap<Profile, ProfileDTO>() .ForMember(d => d.SchoolGrade , op => op.MapFrom(o => o

Automapper - why use Mapper.Initialize?

偶尔善良 提交于 2019-12-04 16:11:09
问题 I wouldn't normally ask this kind of question on here, but unfortunately whilst AutoMapper seems to be a good mapping library, its documentation is woefully bad - there is no XML documentation for the library's methods, and the most official online documentation I could find was this, which is very brisk. If anyone has any better documentation, please let me know. That said, here's the question: why use Mapper.Initialize ? It doesn't seem to be required as you can just use Mapper.CreateMap

Automapper Mapping IEnumerable<SelectListItem> for Dropdown Menu

*爱你&永不变心* 提交于 2019-12-04 16:05:02
Problem I am currently adding automapping to my MVC project and I am stuck. Right now I have a User model used to represent data in the database. I have to map that model to a EditUserModel which will be used when the Edit method is called. The EditUserModel has IEnumerable<SelectListItem> (for a dropdown menu) that I can't seem to figure out how to map. Attempted Solution As of right now I haven't tried to implement anything. I am unsure where the best place for the IEnumerable<SelectListItem> or where to populate it. Right now it is being populated in the Controller. User.cs public class

Automapper with base class and different configuration options for implementations

核能气质少年 提交于 2019-12-04 15:50:01
问题 I have two classes (MVC view model) which inherits from one abstract base class. abstract class BaseModel { } class Car : BaseModel { public string Speed { get; set; } } class Camper : BaseModel { public int Beds { get; set; } } and want to configure AutoMapper with base class, something like: Mapper.CreateMap<BaseModel, DataDestination>(); var someObj = new DataDastination(); Mapper.Map(instanceOfBaseModel, someObj); Here I get error, because Automapper doesn't have configuration of Car or

AutoMapper Exclude Fields

旧城冷巷雨未停 提交于 2019-12-04 15:27:52
问题 I'm trying to map one object to another but the object is quite complex. During development, I'd like the ability to either exclude a bunch of fields and get to them one by one or be able to specify to map only fields I want and increase that as each test succeeds. So: class string field1 string field2 string field3 Now I'd like to map field1 , test, fix and then move onto field2 then field3 . Is this possible? 回答1: .ForMember(dto => dto.field1, opt => opt.Ignore()); 来源: https://stackoverflow