I have a model classes shown below.
public class ModelA
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string DetailId{ get; set; }
public string DetailName { get; set; }
public string DetailDescription { get; set; }
}
public class ModelB
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class ModelC
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ModelB Detail { get; set; }
}
Now, I want to map ModelA to ModelC where
DetailId = Detail.Id
DetailName = Detail.Name
DetailDescription = Detail.Description
Is this possible using Automapper 11.0.0 ?
If possible, how ?
var config = new MapperConfiguration(cfg => {
cfg.CreateMap< ModelA, ModelC >().ForMember(u => u. Detail, o => o.MapFrom(s => s));
});
You want the modelB mapping in modelC,But the structure of modelA is different from that of modelC.
Use this code:
CreateMap<ModelA, ModelC>()
.ForMember(d => d.Detail.Id, src => src.MapFrom(e => e.DetailId))
.ForMember(d => d.Detail.Description, src => src.MapFrom(e => e.DetailDescription))
.ForMember(d => d.Detail.Name, src => src.MapFrom(e => e.DetailName))
.ReverseMap();
Thanks for the answers.
However I solved it already.
MapperConfiguration _config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ModelA, ModelB>()
.ForMember(d => d.DetailId, opt => opt.MapFrom(s => s.Id))
.ForMember(d => d.DetailName , opt => opt.MapFrom(s => s.Name))
.ForMember(d => d.DetailDescription, opt => opt.MapFrom(s => s.Description))
.ReverseMap();
cfg.CreateMap<ModelC, ModelA>()
.ForMember(d => d.DetailId, opt => opt.MapFrom(s => s.Detail.Id ))
.ForMember(d => d.DetailName , opt => opt.MapFrom(s => s.Detail.Name ))
.ForMember(d => d.DetailDescription, opt => opt.MapFrom(s => s.Detail.Description ))
cfg.CreateMap<ModelA, ModelC>()
.ForMember(d => d.Detail, opt => opt.MapFrom(s => Mapper.Map<ModelA, ModelB>(s)))
});
UPDATE
Found a better solution.
Check out the link below.
https://dotnettutorials.net/lesson/mapping-complex-type-to-primitive-type-using-automapper/
Related
I have got a problem using Automapper when conditionally Mapping a table.
Here is an example:
public class DepositsVm : IMapFrom<Deposits>
{
public long DepId { get; set; }
public AddressDto Address { get; set; }
public void Mapping(Profile profile)
{
profile.CreateMap<Deposits, DepositsVm>()
.ForMember(d => d.DepId, s => s.MapFrom(s => s.DepId))
.ForMember(d => d.Address, opt => opt.MapFrom(s => s.ProcessingId != null ? s.DataProcessing.GridCollect.Grid.Address : s.Reduction.DataCollect.Tower.Address));
}
}
This results in an Object reference error.
I can see we can use https://docs.automapper.org/en/stable/Conditional-mapping.html#preconditions but this allow to check for only one condition. I expect to map a table using different join condition in failure and success scenarios.
But this works, because I'm projecting each Address separately. But, this is not desired. Because both are from Same Address Table
public class DepositsVm : IMapFrom<Deposits>
{
public long DepId { get; set; }
public AddressDto GridAddress { get; set; }
public AddressDto TowerAddress { get; set; }
public void Mapping(Profile profile)
{
profile.CreateMap<Deposits, DepositsVm>()
.ForMember(d => d.DepId, s => s.MapFrom(s => s.DepId))
.ForMember(d => d.GridAddress, opt => opt.MapFrom(s => s.DataProcessing.GridCollect.Grid.Address));
.ForMember(d => d.TowerAddress, opt => opt.MapFrom(s => s.Reduction.DataCollect.Tower.Address));
}
}
Either some part of s.DataProcessing.GridCollect.Grid.Address is null, or some part of s.Reduction.DataCollect.Tower.Address is null
I have faced with the strange issue with mapping model to entity with Many-to-Many relationship
I have the following entities and models:
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace AutoMapper.ReproducedExample
{
public class StoreEntity
{
public int Id { get; set; }
public string Name { get; set; }
public List<StoreProductEntity> Products { get; set; } = new List<StoreProductEntity>();
}
public class ProductEntity
{
public int Id { get; set; }
public string Name { get; set; }
public List<StoreProductEntity> Stores { get; set; } = new List<StoreProductEntity>();
}
public class StoreProductEntity
{
public int StoreId { get; set; }
public StoreEntity Store { get; set; }
public int ProductId { get; set; }
public ProductEntity Product { get; set; }
}
public class StoreModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<ProductModel> Products { get; set; } = new List<ProductModel>();
}
public class ProductModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<StoreModel> Stores { get; set; } = new List<StoreModel>();
}
public class CustomProfile : Profile
{
public CustomProfile()
{
CreateMap<StoreModel, StoreEntity>()
.ForMember(d => d.Products,
opt => opt.MapFrom(s => s.Products))
.AfterMap((model, entity) =>
{
foreach (var entityProduct in entity.Products)
{
entityProduct.StoreId = entity.Id;
entityProduct.Store = entity;
}
});
CreateMap<StoreModel, StoreProductEntity>()
.ForMember(entity => entity.StoreId, opt => opt.MapFrom(model => model.Id))
.ForMember(entity => entity.Store, opt => opt.MapFrom(model => model))
.ForMember(entity => entity.ProductId, opt => opt.Ignore())
.ForMember(entity => entity.Product, opt => opt.Ignore());
CreateMap<ProductModel, ProductEntity>()
.ForMember(d => d.Stores,
opt => opt.MapFrom(s => s.Stores))
.AfterMap((model, entity) =>
{
foreach (var entityStore in entity.Stores)
{
entityStore.ProductId = entity.Id;
entityStore.Product = entity;
}
});
CreateMap<ProductModel, StoreProductEntity>()
.ForMember(entity => entity.StoreId, opt => opt.Ignore())
.ForMember(entity => entity.Store, opt => opt.Ignore())
.ForMember(entity => entity.ProductId, opt => opt.MapFrom(model => model.Id))
.ForMember(entity => entity.Product, opt => opt.MapFrom(model => model));
}
}
class Program
{
static void Main(string[] args)
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CustomProfile>();
});
#if DEBUG
configuration.AssertConfigurationIsValid();
#endif
var mapper = configuration.CreateMapper();
var store0 = new StoreModel()
{
Id = 1,
Name = "Store0",
};
var store1 = new StoreModel()
{
Id = 2,
Name = "Store1",
};
var product = new ProductModel()
{
Id = 1,
Name = "Product",
};
store1.Products.Add(product);
product.Stores.Add(store1);
store0.Products.Add(product);
product.Stores.Add(store0);
var store0Entity = mapper.Map<StoreEntity>(store0);
Debug.Assert(store0Entity.Products[0].Product.Stores[0].Store.Id ==
store0Entity.Products[0].Product.Stores[0].Store.Products[0].StoreId);
}
}
}
Mapping pass successfully, but by some reason some deep key is not mapped to the related StoreEntity
The following assertion is failed ...
Debug.Assert(store0Entity.Products[0].Product.Stores[0].Store.Id ==
store0Entity.Products[0].Product.Stores[0].Store.Products[0].StoreId);
Seems like by some reason in depth Store class AutoMapper uses wrong mapped List ... but I am not sure ...
I have figured out how to fixed this issue (thanks to answer on question Automapper creates 2 instances of one object)
For detecting Circular references I can add .PreserveReferences() method for mapping
Here is proper CustomProfile:
public class CustomProfile : Profile
{
public CustomProfile()
{
CreateMap<StoreModel, StoreEntity>()
.ForMember(d => d.Products,
opt => opt.MapFrom(s => s.Products))
.AfterMap((model, entity) =>
{
foreach (var entityProduct in entity.Products)
{
entityProduct.StoreId = entity.Id;
entityProduct.Store = entity;
}
})
.PreserveReferences();
CreateMap<StoreModel, StoreProductEntity>()
.ForMember(entity => entity.StoreId, opt => opt.MapFrom(model => model.Id))
.ForMember(entity => entity.Store, opt => opt.MapFrom(model => model))
.ForMember(entity => entity.ProductId, opt => opt.Ignore())
.ForMember(entity => entity.Product, opt => opt.Ignore());
CreateMap<ProductModel, ProductEntity>()
.ForMember(d => d.Stores,
opt => opt.MapFrom(s => s.Stores))
.AfterMap((model, entity) =>
{
foreach (var entityStore in entity.Stores)
{
entityStore.ProductId = entity.Id;
entityStore.Product = entity;
}
})
.PreserveReferences();
CreateMap<ProductModel, StoreProductEntity>()
.ForMember(entity => entity.StoreId, opt => opt.Ignore())
.ForMember(entity => entity.Store, opt => opt.Ignore())
.ForMember(entity => entity.ProductId, opt => opt.MapFrom(model => model.Id))
.ForMember(entity => entity.Product, opt => opt.MapFrom(model => model));
}
}
But actually I did not know if it is an issue or not, because according the documentation AutoMapper should detect Circular References automatically for AutoMapper version 10.0.0 and seems like this is an issue
Everything was fine, until I added DataTime property to my Model and viewModel. All data is correct, but error occurs anyway.
I think the error appears because of I added PublicationDate.
As I understand, Automapper2.0 should work fine with dates,so I dunno why does it happen
Error occurs here:
Publication viewModelPublication = new Publication(Mapper.Map<PublicationViewModel, Publication>(viewModel));
My AutoMapperConfig:
public class AutoMapperConfig
{
public static void Initialize()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Author, AuthorViewModel>()
.ForMember(a => a.AuthorId, opt => opt.MapFrom(a => a.AuthorId))
.ForMember(a => a.AuthorName, opt => opt.MapFrom(a => a.AuthorName));
cfg.CreateMap<AuthorViewModel, Author>()
.ForMember(a => a.AuthorId, opt => opt.MapFrom(a => a.AuthorId))
.ForMember(a => a.AuthorName, opt => opt.MapFrom(a => a.AuthorName));
cfg.CreateMap<Publication, PublicationViewModel>()
.ForMember(p => p.PublicationId, opt => opt.MapFrom(p => p.PublicationId))
.ForMember(p => p.PublicationName, opt => opt.MapFrom(p => p.PublicationName))
.ForMember(p => p.Pages, opt => opt.MapFrom(p => p.Pages))
.ForMember(p => p.Publisher, opt => opt.MapFrom(p => p.Publisher))
.ForMember(p => p.PublicationDate, opt => opt.MapFrom(p => p.PublicationDate))
.ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription))
.ForMember(p => p.Authors, opt => opt.MapFrom(p => p.Authors.ToList()));
cfg.CreateMap<PublicationViewModel, Publication>()
.ForMember(p => p.PublicationId, opt => opt.MapFrom(p => p.PublicationId))
.ForMember(p => p.PublicationName, opt => opt.MapFrom(p => p.PublicationName))
.ForMember(p => p.Pages, opt => opt.MapFrom(p => p.Pages))
.ForMember(p => p.Publisher, opt => opt.MapFrom(p => p.Publisher))
.ForMember(p => p.PublicationDate, opt => opt.MapFrom(p => p.PublicationDate))
.ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription))
.ForMember(p => p.Authors, opt => opt.MapFrom(p => p.Authors));
});
}
}
My models:
public class PublicationViewModel
{
public PublicationViewModel()
{
Authors = new List<AuthorViewModel>();
}
public int PublicationId { get; set; }
[Display(Name = "Publication Name")]
public string PublicationName { get; set; }
public int Pages { get; set; }
public string Publisher { get; set; }
[Display(Name = "Publication Date")]
public DateTime PublicationDate { get; set; }
public SubscriptionViewModel Subscription { get; set; }
[UIHint("AuthorsEditor")]
public virtual List<AuthorViewModel> Authors { get; set; }
}
public class Publication
{
public Publication()
{
}
public Publication(Publication publication)
{
PublicationId = publication.PublicationId;
PublicationName = publication.PublicationName;
Pages = publication.Pages;
Publisher = publication.Publisher;
Subscription = publication.Subscription;
PublicationDate = publication.PublicationDate;
Authors = publication.Authors;
}
public int PublicationId { get; set; }
public string PublicationName { get; set; }
public int Pages { get; set; }
public string Publisher { get; set; }
public DateTime PublicationDate { get; set; }
public Subscription Subscription { get; set; }
public virtual ICollection<Author> Authors { get; set; }
}
I've found a problem. I forgot to send id from my subscriptionViewModel(Subscription is an enum type).
.ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription.SubscriptionId))//here
I'm making Forum system, I have SubCategoryThreadsViewModel in which I'm trying to map LastComment and Date of last post for every thread. This is my code:
public class SubCategoryThreadsViewModel : IHaveCustomMappings
{
public string Title { get; set; }
public string Description { get; set; }
public IEnumerable<Thread> Threads { get; set; }
public ThreadInfoSubCategoryViewModel ThreadInfoSubCategoryViewModel { get; set; }
public void CreateMappings(IConfiguration configuration)
{
configuration.CreateMap<Thread, SubCategoryThreadsViewModel>()
.ForMember(m => m.Title, opt => opt.MapFrom(t => t.SubCategory.Title))
.ForMember(m => m.Description, opt => opt.MapFrom(t => t.SubCategory.Description))
.ForMember(m => m.Threads, opt => opt.MapFrom(t => t.SubCategory.Threads))
.ForMember(m => m.ThreadInfoSubCategoryViewModel, opt => opt.MapFrom(t => new ThreadInfoSubCategoryViewModel()
{
LastCommentBy = t.Posts.Select(a => a.Author.UserName),
DateOfLastPost = t.Posts.Select(a => a.CreatedOn.ToString()),
}))
.ReverseMap();
}
The code
.ForMember(m => m.ThreadInfoSubCategoryViewModel, opt => opt.MapFrom(t => new ThreadInfoSubCategoryViewModel()
{
LastCommentBy = t.Posts.Select(a => a.Author.UserName),
DateOfLastPost = t.Posts.Select(a => a.CreatedOn.ToString()),
}))
is working but only when property ThreadInfoSubCategoryViewModel is not Ienumerable as in the code above, and inside are two IEnumerable strings.
public class ThreadInfoSubCategoryViewModel
{
public IEnumerable<string> LastCommentBy { get; set; }
public IEnumerable<string> DateOfLastPost { get; set; }
}
This works, but I want ThreadInfoSubCategoryViewModel to be Ienumerable, and in the class properties to be string for easy foreach.
I have tried to make it IEnumerable, but with current automapper code it doesn't work.
You need to manually map the member to an IEnumerable<ThreadInfoSubCategoryViewModel> rather than a single object.
I assume each Post in t.Posts represents one ThreadInfoSubCategoryViewModel, so a simple Select() should do it:
public IEnumerable<ThreadInfoSubCategoryViewModel> ThreadInfoSubCategoryViewModel { get; set; }
...
.ForMember(m => m.ThreadInfoSubCategoryViewModel, opt => opt.MapFrom(t =>
t.Posts.Select(p => new ThreadInfoSubCategoryViewModel()
{
LastCommentBy = p.Author.UserName,
DateOfLastPost = p.CreatedOn.ToString()
})
))
I have something like this:
public class DomainEntity
{
public string Name { get; set; }
public string Street { get; set; }
public IEnumerable<DomainOtherEntity> OtherEntities { get; set; }
public IEnumerable<DomainAnotherEntity> AnotherEntities { get; set; }
}
public class ApiEntity
{
public string Name { get; set; }
public string Street { get; set; }
public int OtherEntitiesCount { get; set; }
}
And following mapper configuration:
Mapper.Configuration.AllowNullCollections = true;
Mapper.CreateMap<DomainEntity, ApiEntity>().
ForSourceMember(e => e.OtherEntities, opt => opt.Ignore()).
ForSourceMember(e => e.AntherEntities, opt => opt.Ignore()).
ForMember(e => e.OtherEntitiesCount, opt => opt.MapFrom(src => src.OtherEntities.Count()));
Mapper.CreateMap<ApiEntity, DomainEntity>().
ForSourceMember(e => e.OtherEntitiesCount, opt => opt.Ignore()).
ForMember(e => e.OtherEntities, opt => opt.Ignore()).
ForMember(e => e.AnotherEntities, opt => opt.Ignore());
To get the ApiEntity from the DomainEntity I'm using var apiEntity = Mapper.Map<DomainEntity, ApiEntity>(myDomainEntity);
To get the merged DomainEntity from an ApiEntity I'm using var domainEntity = Mapper.Map(myApiEntity, myDomainEntity);
But when using this, the properties OtherEntities and AnotherEntities are set to null - even when they had values before calling the mapping from myApiEntity to myDomainEntity. How can I avoid this so they really merge and not just replacing values?
Thanks for any help.
I think you're looking for UseDestinationValue instead of Ignore:
Mapper.CreateMap<ApiEntity, DomainEntity>().
ForSourceMember(e => e.OtherEntitiesCount, opt => opt.UseDestinationValue()).
ForMember(e => e.OtherEntities, opt => opt.UseDestinationValue()).
ForMember(e => e.AnotherEntities, opt => opt.UseDestinationValue());