Good afternoon, I have an interesting problem with Automapperom
In my projects I use Automapper 4.1.1, with a lot of code inside.
The solution consists of four projects.
- Core - services and extensions
- DataLayer - repository and avtomapper
- MVC - standard web project MVC
- WebApi - Web API Service (recently added)
To work with the data I use
- Entity FrameWork 6.1.1
- AutoMapper 4.1.1
The database is local, the connection string is stored in web.config (in both projects)
I have two models, User is an object base, and UserModel is a business model
In the DATA level, my class looks like this.
namespace DataLayer { internal static class Mapping { static Mapping() { RegisterMappings(); } public static TDestination Map<TSource, TDestination>(TSource source, TDestination destination) { return Mapper.Map(source, destination); } private static void RegisterMappings() { try { Mapper.AllowNullDestinationValues = true; //Mapping instruction for User Mapper.CreateMap<User, UserModel>().IgnoreAllPropertiesWithAnInaccessibleSetter().ReverseMap(); Mapper.AssertConfigurationIsValid(); } } } at basic level
public AuthenticationResult Login(string login, string password) { if (string.IsNullOrEmpty(login)) throw new ArgumentNullException(nameof(login)); if (string.IsNullOrEmpty(password)) throw new ArgumentNullException(nameof(password)); using (var db = new Context()) { try { //Here my UserDbEntity model, and it already connected from Context var dbUser = db.Users.AsNoTracking().FirstOrDefault(u => u.Login == login && u.IsActive && !u.IsDeleted); //mapping model var model = Mapping.Map(dbUser, new UserModel()); //Some result logic here return result; } } } Links to other projects are configured in the API and MVC projects and when I call from the MVC project
CoreLayer.Login(string login, string pass) it returns the correct data to me, but when I call the same instruction through the API
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type ================================================================== ServiceModel ->Service (Source member list) Core.Models.ServiceModel -> Data.DB.Service (Source member list) Unmapped properties: Specialization ServiceTechnic ServiceTools RefRescueWorkTypes All objects that caused the exception are complex (the model is inside the model) and when working with the MVC project, the mapping runs without problems, and an exception is given with the API.