automapper

Automapper skip null values for all classes

ε祈祈猫儿з 提交于 2021-01-28 08:12:45
问题 In this link we can set Automapper to skip null value from source object. Can we configure the condition for all classes automatically? Thanks. 回答1: You can do something like: Mapper.Initialize(cfg => { cfg.ForAllMaps((typeMap, map) => map.ForAllOtherMembers(opt => opt.Condition((src, dest, srcMem, destMem) => src != null))); } I used ForAllOtherMembers so that it does not override your other conditions that you may declare individually. 来源: https://stackoverflow.com/questions/32621702

AutoMapper cannot mapping Reference or using UseDestinationValue

你说的曾经没有我的故事 提交于 2021-01-28 06:59:34
问题 Code First class Woman { public string Name { get; set; } } class Family { public string SweetName { get; set; } public Woman Wife { get; set; } } class MyProfile : Profile { public MyProfile() { CreateMap<Woman, Woman>(); CreateMap<Family, Family>() .ForMember(d => d.Wife, o => o.UseDestinationValue()); } } class Program { static void main() { // Initialize var ce = new MapperConfigurationExpression(); ce.AddProfile(new MyProfile()); var mapper = new MapperConfiguration(ce).CreateMapper();

automapper project into a larger object

左心房为你撑大大i 提交于 2021-01-28 03:56:21
问题 With Automapper, is it possible to project a smaller object onto a larger one? For example, a controller accepts data as a ViewModel instance. I would then need to create a record in a database. So I would project this View Model onto a Domain Model. Once I have a Domain Model instance populated with View Model data I would then manually populate the additional fields in the Domain Model before storing data in the database. Is it possible to do so? Thanks. 回答1: Yes this is perfectly possible.

automapper project into a larger object

廉价感情. 提交于 2021-01-28 03:30:39
问题 With Automapper, is it possible to project a smaller object onto a larger one? For example, a controller accepts data as a ViewModel instance. I would then need to create a record in a database. So I would project this View Model onto a Domain Model. Once I have a Domain Model instance populated with View Model data I would then manually populate the additional fields in the Domain Model before storing data in the database. Is it possible to do so? Thanks. 回答1: Yes this is perfectly possible.

AutoMapper skip all members on source that are null

ぃ、小莉子 提交于 2021-01-27 23:04:10
问题 given this mapping _mapper.Map(personDto, person, opt => opt.ConfigureMap() .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null)) ); person before mapping FirstName: "John" LastName: "Doe" Email: "John.Doe@gmail.com" personDto before mapping FirstName: "Jim" LastName: "Denver" Email: NULL expected output person FirstName: "Jim" LastName: "Denver" Email: "John.Doe@gmail.com" actual output person FirstName: "Jim" LastName: "Denver" Email: NULL mapper came from this

How to configure Automapper 9 to ignore Object-Properties if object is null but map if not null

跟風遠走 提交于 2021-01-27 22:34:23
问题 I´ve tried a lot, but I can´t find what I´m really looking for. This is my case: I have an EF-Core entity with navigation-properties and a viewModel: public class SomeEntity { public Guid Id { get; set; } public virtual NestedObject NestedObject { get; set; } public DateTime Created { get; set; } public DateTime Modified { get; set; } } public class SomeEntityViewModel { public Guid Id { get; set; } public string NestedObjectStringValue { get; set; } public int NestedValueIntValue { get; set;

How can I use Automapper to map an object to an unknown destination type?

五迷三道 提交于 2021-01-27 04:54:09
问题 Consider the following scenario. I have a number of classes that share a common base class and I have defined an automapper mapping for each derived class. Something like this: class A : Base {} class B : Base {} class ContractA : ContractBase {} class ContractB : ContractBase {} void Foo() { Mapper.CreateMap<A, ContractA>(); Mapper.CreateMap<B, ContractB>(); } So far so good. But now I want to create a method like this: ContractBase Foo() { Base obj = GetObject(); return Mapper.??? } The

How to mock Automapper (IMapper) in controller

不羁的心 提交于 2021-01-22 05:43:13
问题 I am trying to write a unit test for my existing MVC Web Aplication. In that I am facing some problem in automapper ( IMapper ) Whenever am using map function it returns null value. My Controller Code: public class UserAdministrationController : BaseController { private readonly iUserService _userService; private readonly IMapper _mapper; public NewsController(iUserService userService, IMapper mapper) { _userService = userService; _mapper = mapper; } public ActionResult Create(int CompanyID =

Automapper conditional map from multiple source fields

南笙酒味 提交于 2021-01-20 06:59:52
问题 I've got a source class like the following: public class Source { public Field[] Fields { get; set; } public Result[] Results { get; set; } } And have a destination class like: public class Destination { public Value[] Values { get; set; } } So I want to map from EITHER Fields or Results to Values depending on which one is not null (only one will ever have a value). I tried the following map: CreateMap<Fields, Values>(); CreateMap<Results, Values>(); CreateMap<Source, Destination>()

Mapping one type to another

帅比萌擦擦* 提交于 2021-01-07 01:36:17
问题 Let's say I have the following types. type Contract struct { Id string `json:"id" gorm:"column:uuid"` Name string `json:"name" gorm:"column:name"` Description string `json:"descr" gorm:"column:descr"` ContractTypeId int `json:"contract_type_id" gorm:"column:contract_type_id"` } type ContractModel struct { Id string `json:"id" gorm:"column:uuid"` Name string `json:"name" gorm:"column:name"` Description string `json:"descr" gorm:"column:descr"` } I have a SQL query using gorm to scan results