automapper

Mapping the 'count' on child list property in AutoMapper

℡╲_俬逩灬. 提交于 2019-12-02 12:40:06
I am working in an ASP.NET MVC 4 application, and have something similar to this for my domain. public class Party { public Cake PartyCake { get; set; } public List<Candles> CakeCandles { get; set; } } I want to map that to the PartyVM which looks like so: public class PartyVM { public string PartyCakeName { get; set; } public int CandlesCount { get; set; } } How can I tell AutoMapper to work out the Count of the list when doing its mappings? I have this, but all this does is tell it to ignore! Mapper.CreateMap<Party, PartyVM>() .ForMember(dest => dest.CandlesCount, opt => opt.Ignore());

Dependency Injection Container for each Class Library

巧了我就是萌 提交于 2019-12-02 10:44:41
问题 So, this is the first time I'm dealing with DI, please correct me if I misunderstood the whole DI thingy. These are few of my projects: Web Application/ Web API Project - Depends on Service Class + inject Automapper (Configuration only applicable for current project) Service (Class Library) - Depends on Data Class + inject Automapper (Configuration only applicable for current project) Data (Class Library) My intention was to have each project having its own DI container (says Unity DI). I'm

Automapper Projection with Linq OrderBy child property error

…衆ロ難τιáo~ 提交于 2019-12-02 09:31:40
问题 I am having an issue using an AutoMapper (version 5.1.1) projection combined with a Linq OrderBy Child property expression. I am using Entity Framework Core (version 1.0.0). I am getting the following error: "must be reducible node" My DTO objects are as follows public class OrganizationViewModel { public virtual int Id { get; set; } [Display(Name = "Organization Name")] public virtual string Name { get; set; } public virtual bool Active { get; set; } public virtual int OrganizationGroupId {

EF6 AutoMapper6 Parent/Child different behavior

[亡魂溺海] 提交于 2019-12-02 08:58:19
问题 I just updated an entire WCF app from EF4 / AutoMapper 1.1 to EF6 / AutoMapper 6.0.0.2 and the behavior is not completely the same. This doesn't work for me : Entity Framework - Add Child Entity Before : child.Parent = parentObject OR parentObject.Children.Add(child) had the same result in real time (while debugging == before SaveChanges), so I decided to use child.Parent = parentObject for the readability. child.Parent = parentObject added a child in parentObject automatically. The child was

Automapper - Inheritance mapper not working with type converter

感情迁移 提交于 2019-12-02 08:50:46
Can't use Mapping Inheritance and TypeConverter together. I have this code: /* BaseClassTypeConverter.cs */ public class BaseClassTypeConverter : ITypeConverter<SourceClass, BaseClass> { public BaseClass Convert(ResolutionContext context) { if (context == null || context.IsSourceValueNull) return null; var src = (SourceClass)context.SourceValue; return new BaseClass() { CommonAttr = src.SourceAttr }; } } /* AutoMapperConfig.cs */ public static class AutoMapperConfig { public static void RegisterMappings() { AutoMapper.Mapper.Initialize(config => { config .CreateMap<SourceClass, BaseClass>()

Automapper: map child property with existing mapper using instance API

我的未来我决定 提交于 2019-12-02 08:31:57
I'm trying to map one complex object to another using instance API: var config = new MapperConfiguration(cfg => { cfg.CreateMap<Student, PersonType>(); cfg.CreateMap<Professor, PersonType>(); cfg.CreateMap<Branch, BranchType>() .ForMember(x => x.Departments, opt => opt.MapFrom(src => new DepartmentType[] { new DepartmentType { Students = Mapper.Map<Student[], PersonType[]> (src.Students), Professors = Mapper.Map<Professor[], PersonType[]> (src.Professors), Name = src.DepartmentName } })) .ForMember(x => x.Name, opt => opt.MapFrom(src => src.Name)) .ForAllMembers(opts => opts.Condition((src,

How to use AutoMapper to map a DataRow to an object in a WCF service?

狂风中的少年 提交于 2019-12-02 04:54:01
问题 I have a WCF service that calls a stored procedure and returns a DataTable. I would like to transform the DataRows to custom object before sending to the consumer but can't figure out how to do so. Lets say I retrieved a customer from a stored procedure. To keep things short here is the customer via DataRow: string lastName = dt.Rows[0]["LastName"].ToString(); string firstName = dt.Rows[0]["FirstName"].ToString(); string age = System.Convert.ToInt32(dt.Rows[0]["Age"].ToString()); I can

Dependency Injection Container for each Class Library

我的未来我决定 提交于 2019-12-02 03:17:45
So, this is the first time I'm dealing with DI, please correct me if I misunderstood the whole DI thingy. These are few of my projects: Web Application/ Web API Project - Depends on Service Class + inject Automapper (Configuration only applicable for current project) Service (Class Library) - Depends on Data Class + inject Automapper (Configuration only applicable for current project) Data (Class Library) My intention was to have each project having its own DI container (says Unity DI). I'm not sure that each project can have its own DI container. I have read some of the article, showing that

AutoMapper TypeConverter mapping nullable type to not-nullable type

送分小仙女□ 提交于 2019-12-02 02:52:23
I'm using AutoMapper, and I've registered a TypeConverter to map nullable long values to long values like so: public class NullableLongToLongConverter : TypeConverter<long?, long> { protected override long ConvertCore(long? source) { return source ?? 0; } } This works fine, and automatically picks up any nullable longs being converted to longs. However, I've got some other maps which want to 'convert' nullable longs to nullable longs. These also end up using this type converter. For example, both properties are getting set to 0 in the code below, but I'd expect NullableLong to be set to null.

AutoMapper - Conditional Mapping for Type

橙三吉。 提交于 2019-12-02 01:32:46
I'd like to do something like the following and I'm wondering if someone knows how to do it: Mapper.CreateMap<Worksheet, V2WorksheetModel>().If(pWorksheet=> pWorksheet.VisitLevel == 2); Mapper.CreateMap<Worksheet, V3WorksheetModel>().If(pWorksheet=> pWorksheet.VisitLevel == 3); Worksheet entityVisit2 = MyService.GetWorksheetByID(100); //visit level 2 Worksheet entityVisit3 = MyService.GetWorksheetByID(150); //visit level 3 WorksheetModelBase modelBase1 = Mapper.Map(entityVisit2); WorksheetModelBase modelBase2 = Mapper.Map(entityVisit3); Assert.True(modelBase is V2WorksheetModel); Assert.True