automapper

Automapper Mapping for Localization Resolver in a Multi-Language Website

此生再无相见时 提交于 2019-12-22 12:05:11
问题 I found a way to use Automapper for Language mapping based on the active Culture. The question is if it's possible to build a generic Resolver to map all the models that use the Resolver. In this Case, the models to map have always the same properties, Id and Name (including the language properties Name_PT, Name_FR and Name_EN): // MODELS public class MakeDto { // Primary properties public int Id { get; set; } public string Name { get; set; } public string Name_PT { get; set; } public string

'entity of the same type already has the same primary key value' error using AutoMapper

五迷三道 提交于 2019-12-22 10:56:55
问题 When I use AutoMapper, this Error occured: Attaching an entity of type 'MyProject.DAL.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added'

Using AutoMapper to load entities from the database?

二次信任 提交于 2019-12-22 10:54:39
问题 Most of what I've read (e.g. from the author) indicates that AutoMapper should be used to map an an entity to a DTO. It should not load anything from the database. But what if I have this: public class Customer { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Order> Orders { get; set; } } public class CustomerDto { public int Id { get; set; } public string Name { get; set; } public IEnumerable<int> OrderIds { get; set; } // here is the problem } I need

Configuring Automapper in N-Layer application

懵懂的女人 提交于 2019-12-22 10:43:17
问题 I have an N-Layer application as shown below MyApp.Model - contains edmx and data models MyApp.DataAccess - Repositories with EF MyApp.Domain - Domain/business models MyApp.Services - services(class library) MyApp.Api - ASP.NET Web API I am using Unity as my IoC container and Automapper for OO mapping. My DataAccess layer references Model layer which contains all my Data objects. I do not want to refer my model project in my Api layer. So returning DomainObjects (business models) from service

AutoMapper Ignore on child collection property

冷暖自知 提交于 2019-12-22 09:55:18
问题 I am trying to map object's of the same type which have a collection of child objects and am finding that Ignore() applied to properties on the child object seem to be umm... ignored! Here's a unit test which demonstrates the problem. class A { public int Id { get; set; } public string Name { get; set; } public ICollection<B> Children { get; set; } } class B { public int Id { get; set; } public string Name { get; set; } } [TestClass] public class UnitTest1 { [TestInitialize()] public void

Is Automapper supposed to work with private setters OOB?

你说的曾经没有我的故事 提交于 2019-12-22 06:38:06
问题 I have a nested child class with public properties with private setters. In the parent class I am able to use Automapper to map to the child class and the values of the private setters are being set. Everything I've read points to Automapper not supporting this and requiring a custom constructor in the child class to populate private setters. Is the current version using reflection or something to map the private setters? The setters are definitely private because in the parent class I am

Is it a good practice to mock Automapper in unit tests?

陌路散爱 提交于 2019-12-22 05:27:13
问题 There is this codebase where we use automapper and have 2 layers, Domain and Service . Each has its object for data representation, DomainItem and ServiceItem . The service gets data from domain, the uses constructor injected automapper instance to map class Service { public ServiceItem Get(int id) { var domainItem = this.domain.Get(id); return this.mapper.Map<DomainItem, ServiceItem>(domainItem); } } Assume best practices, so mapper has no side-effects and no external dependencies. You'd

Automapper Mapping IEnumerable<SelectListItem> for Dropdown Menu

谁都会走 提交于 2019-12-21 21:49:11
问题 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

AutoMapper isn't recognizing profile-specific prefixes

我的未来我决定 提交于 2019-12-21 20:08:17
问题 I'm trying to use AutoMapper to take data from a class that has prefixes before property names and map it to a second class that doesn't have those prefixes. However, I don't necessarily want it to always strip out that prefix: I just want it to do it for this particular mapping. My source class looks like this: public class AdvancedSearchFilterDataModel { // .... public string ServiceMeterNumber { get; set; } // .... } My destination class looks like this: [DataContract] public class

Automapper many-to-many stackoverflowexception

旧城冷巷雨未停 提交于 2019-12-21 18:43:21
问题 I'm getting a stack overflow for the following mapping: Mapper.CreateMap<Parent, ParentViewModel>() .ForMember(x => x.Children, o => o.MapFrom(x => x.Children.ConvertToChildrenViewModel())); Mapper.CreateMap<Children, ChildrenViewModel>() .ForMember(x => x.Parents, o => o.MapFrom(x => x.Parents.ConvertToParentViewModel())); I understand why this is happening, clearly an infinite loop here. How am I supposed to get this to work in automapper? I need parents to know about their children and