automapper

How to expose part of Entity as DataContract?

Deadly 提交于 2019-12-02 00:13:46
Up to now, when working with WCF, I have always been exposing the whole either EF generated entities, or POCOs(by modifying the T4 template to include DataContract and DataMember on POCOs and properties) as DataContract. Now, I have come across a situation that I cannot expose the whole thing, and need to explicitly specify my DataContract to be a subset of the entities. It worth saying that one of my entities is something like below: And I want to just expose Id, Name, CategoryId, Price. Insert/Update of rest of the fields ( ActiveFrom , InactiveDate , Supported ) is something that will be

Automapper Custom Resolver - Inject Repository into constructor

别来无恙 提交于 2019-12-01 21:23:34
问题 I am trying to create a custom resolver for automapper which needs to access one of my data repositories to retreive the logged in users account. Here is my code so far... public class FollowingResolver : ValueResolver<Audio, bool> { readonly IIdentityTasks identityTasks; public FollowingResolver(IIdentityTasks identitTasks) { this.identityTasks = identitTasks; } protected override bool ResolveCore(Audio source) { var user = identityTasks.GetCurrentIdentity(); if (user != null) return user

Automapper ProjectTo adds ToList into child properties

梦想与她 提交于 2019-12-01 21:18:28
I use projection to map the Entity classes to DTOs using Entity Framework Core. However, projection adds ToList into child collection properties and this slows down the query a lot. Company Entity: public class Company { public Company() { Employees = new List<CompanyEmployee>(); } public string Address { get; set; } public virtual ICollection<CompanyEmployee> Employees { get; set; } ... } Company DTO: public class CompanyDTO { public CompanyDTO() { CompanyEmployees = new List<EmployeeDTO>(); } public string Address { get; set; } public List<EmployeeDTO> CompanyEmployees { get; set; } ... }

Conditional projection using AutoMapper

◇◆丶佛笑我妖孽 提交于 2019-12-01 19:47:37
Say I have a 'Comment' property on a 'Message' class. I also have 2 class properties which have a 'Body' property. If the class has either of the class properties set, I want AutoMapper to project the Body property into the comment property of the model, otherwise use the normal comment property on the message class. e.g. public class Message { public string Comment { get; set; } public Inbound? InboundMessage { get; set; } public Outbound? OutboundMessage { get; set; } } public class Inbound { public string Body { get; set; } } public class Outbound { public string Body { get; set; } } public

AutoMapper flatten nested collections

十年热恋 提交于 2019-12-01 18:44:35
I try to figure out how to flatten a collection of Merchants, each containing a collection of Orders to a flat List of OrderViewModels. Here my DTO: public class Merchant { public string MerchantName { get; set; } public List<Order> Orders { get; set; } } public class Order { public string OrderId { get; set; } } And Here's the view model: public class OrderViewModel { public string MerchantName { get; set; } public string OrderId { get; set; } } My Goal is to flatten a List<Merchant> to a List<OrderViewModel> whereas the following test structure should result in 6 view models: var myMerchants

automapper class and nested class map to one class

Deadly 提交于 2019-12-01 18:30:50
i have written alot of description but i figured making a picture will make my problem clearer than words i have written this to map but it throws an exception Mapper.CreateMap<GenericStory, GenericStoryDisplayViewModel>().ForMember( gs => gs.StoryBody,dest => dest.MapFrom( gs => gs)); Trying to map StoryWriting.Web.Models.GenericStory to StoryWriting.Web.ViewModels.StoryBodyViewModel. Using mapping configuration for StoryWriting.Web.Models.GenericStory to StoryWriting.Web.ViewModels.GenericStoryDisplayViewModel Destination property: StoryBody Missing type map configuration or unsupported

Automapper Custom Resolver - Inject Repository into constructor

淺唱寂寞╮ 提交于 2019-12-01 18:24:12
I am trying to create a custom resolver for automapper which needs to access one of my data repositories to retreive the logged in users account. Here is my code so far... public class FollowingResolver : ValueResolver<Audio, bool> { readonly IIdentityTasks identityTasks; public FollowingResolver(IIdentityTasks identitTasks) { this.identityTasks = identitTasks; } protected override bool ResolveCore(Audio source) { var user = identityTasks.GetCurrentIdentity(); if (user != null) return user.IsFollowingUser(source.DJAccount); return false; } } However I am getting this error: FollowingResolver'

UseDestinationValue only when destination property is not null

試著忘記壹切 提交于 2019-12-01 17:31:17
How to configure AutoMapper mapping when I want to use behaviour from UseDestinationValue method, but only when destination property is NOT null . Something like that: Mapper.CreateMap<Item, ItemViewModel>() .ForMember(x => x.Details, _ => _.UseDestinationValue(dontUseWhenNullDestination: true)) EDIT class ItemDetails { public string Info { get; set; } public string ImportantData { get; set; } // only in Domain, not in ViewModel } class Item { public ItemDetails Details { get; set; } } class ItemDetailsViewModel { public string Info { get; set; } } class ItemViewModel { public

How can you deal with circular references when mapping?

荒凉一梦 提交于 2019-12-01 17:31:01
问题 Take this database for example Employee id - int (PK) name - varchar Salary id - int (PK) employee_id - int (FK) amount - float Entity Framework will generate models similar to these: public partial class Employee { public Employee() { this.Salaries = new HashSet<Salary>(); } public int id { get; set; } public string name { get; set; } } public partial class Salary { public int id { get; set; } public int employee_id { get; set; } public float amount { get; set; } public Employee employee {

Extra iterations in a foreach in an AutoMapper map

Deadly 提交于 2019-12-01 16:05:56
For some reason, a loop that I use in an AutoMapper map definition is iterating more than it should. The map definition: Mapper.CreateMap<AdminGameEditModel, Game>() .BeforeMap((s, d) => { foreach (var platId in s.PlatformIDs) { Platform newPlat = _gameRepository.GetPlatform(platId); d.Platforms.Add(newPlat); } }) .ForMember(dest => dest.BoxArtPath, opt => opt.Ignore()) .ForMember(dest => dest.IndexImagePath, opt => opt.Ignore()) .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => String.Join("|", src.Cons))) .ForMember(dest => dest.Pros, opt => opt.MapFrom(src => String.Join("|", src.Pros