automapper

AutoMapper: Mapping objects with interface properties

你。 提交于 2019-12-24 00:20:13
问题 my current task needs to pay attention on mapping between different object types and so I recognized the very nice AutoMapper library. So far easy to handle but these different objects contains complex interface type properties. Let me show you a code snippet: public inferface IInterface { string TextProperty { get; set;} } public class A : IInterface { string TextProperty { get; set; } } public class B : IInterface { string TextProperty { get; set; } } public inferface IComplexInterface {

How to use Ignore globally in AutoMapper?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 00:06:21
问题 Here is how it looks like right now. DestinationA and DestinationB are derived from some DestinationBase class. And I need to ignore some common properties for all these derived class. Is there anyway to apply these ignore options globally without having to repeat for all derived destination classes? Mapper.CreateMap<SourceA, DestinationA>() .ForMember(d => d.PropA, opt => opt.Ignore()) .ForMember(d => d.PropB, opt => opt.Ignore()) .ForMember(d => d.PropC, opt => opt.Ignore()); Mapper

Automapper entity framework foreign key is null

假如想象 提交于 2019-12-23 19:23:14
问题 I am trying to update the database using entity framework, I map my entities to viewmodels using automapper, and map it back the same way: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([FromJson] MyCVViewModel model) { var userId = User.Identity.GetUserId(); //find the cv CV cv = repository.FindCV(model.CVId); //auto mapper mapping Mapper.CreateMap<MyCVViewModel, CV>(); Mapper.CreateMap<MyCompanyViewModel, Company>(); cv = Mapper.Map<MyCVViewModel, CV>(model, cv); //edit

How to resolve - unable to cast Generic.List to Linq.IQueryable using Automapper?

拟墨画扇 提交于 2019-12-23 12:43:05
问题 I'm getting the following error in using a DTO and EF model: Unable to cast object of type 'System.Collections.Generic.List 1[Project.Core.UI.Models.ContentTypes]' to type 'System.Linq.IQueryable 1[Project.Core.UI.Models.ContentTypes] Bootstrapped: Mapper.CreateMap<ContentType, Project.Core.UI.Models.ContentTypes>(); In an OData controller method public IQueryable<ContentTypes> Get() {...} , using: var result = Mapper.Map<IQueryable<ContentType>, IQueryable<ContentTypes>>(_repository.Query()

Automapper: how to map nested object?

时光怂恿深爱的人放手 提交于 2019-12-23 10:05:04
问题 I am struggling with the Automapper syntax. I have a List of PropertySurveys, each containing 1 Property. I wish to map each item on the collection into a new object which combines the 2 classes. So my code looks like; var propertySurveys = new List<PropertyToSurveyOutput >(); foreach (var item in items) { Mapper.CreateMap<Property, PropertyToSurveyOutput >(); var property = Mapper.Map<PropertyToSurvey>(item.Property); Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput >(); property =

Mapper not initialized , error with my unit test Core.Net 2.0

拟墨画扇 提交于 2019-12-23 09:36:22
问题 I have a WebApi done in Core.net 2.0, with UOW , and automapper. Everything is working fine, but now I want to implement Unit Test with Nunit, and I have this error of automapper Message: System.InvalidOperationException : Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource

How to stop Automapper from mapping to parent class when child class was requested

烂漫一生 提交于 2019-12-23 08:53:40
问题 I am working on implementing AutoMapper in our service and am seeing a very confusing issue in our unit tests. First off this issue involves the following objects and their respective maps: public class DbAccount : ActiveRecordBase<DbAccount> { // this is the ORM entity } public class Account { // this is the primary full valued Dto } public class LazyAccount : Account { // this class as it is named doesn't load the majority of the properties of account } Mapper.CreateMap<DbAccount,Account>()

How to stop Automapper from mapping to parent class when child class was requested

这一生的挚爱 提交于 2019-12-23 08:52:32
问题 I am working on implementing AutoMapper in our service and am seeing a very confusing issue in our unit tests. First off this issue involves the following objects and their respective maps: public class DbAccount : ActiveRecordBase<DbAccount> { // this is the ORM entity } public class Account { // this is the primary full valued Dto } public class LazyAccount : Account { // this class as it is named doesn't load the majority of the properties of account } Mapper.CreateMap<DbAccount,Account>()

Automapper sets an array property to a zero-length array rather than null

主宰稳场 提交于 2019-12-23 06:47:05
问题 I'm using Automapper to copy values from one instance to another, and I'm finding that if the class has an array property, and the source instance has the property set to null , Automapper sets the destination property to a zero-length array instead of null as I expected. Is there a way to configure Automapper to set the destination to null when the source is null ? In case my explanation is unclear, the following code illustrates what I'm trying to describe: public class Test { public byte[]

AutoMapper not mapping correctly when same type

▼魔方 西西 提交于 2019-12-23 05:47:07
问题 I got a problem with the AutoMapper (3.1.1 on .NET 4.5) library is very strange, i will appreciate your help. I got four objects: public class UserDetail { public int Salary { get; set; } public Address House { get; set; } public Address Office { get; set; } } public class UserDetailEntity { public int Salary { get; set; } public Guid HouseId { get; set; } public Guid? OfficeId { get; set; } public virtual AddressEntity House { get; set; } public virtual AddressEntity Office { get; set; } }