Cannot map from ViewModel to ApplicationUser in AutoMapper 5
Asked Answered
A

0

1

I have a Student class inherited from ApplicationUser base class (ASP.NET Identity) and there is a ViewModel of it called StudentViewModel as shown below:

Entity Classes:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin,
                                     ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
    public string Name { get; set; }
    public string Surname { get; set; } 
    //code omitted for brevity
}

public class Student: ApplicationUser
{     
    public int? Number { get; set; }
}

ViewModel:

public class StudentViewModel
{
    public int Id { get; set; }     
    public int? Number { get; set; }
    //code omitted for brevity
}

I use the following method in order to update a Student by mapping StudentViewModel to ApplicationUser in the Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Update([Bind(Exclude = null)] StudentViewModel model)
{
    //Mapping StudentViewModel to ApplicationUser ::::::::::::::::
    var student = (Object)null;

    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<StudentViewModel, Student>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForAllOtherMembers(opts => opts.Ignore());
    });

    Mapper.AssertConfigurationIsValid();
    student = Mapper.Map<Student>(model);
    //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    //Then I want to pass the mapped property to the UserManager's Update method:
    var result = UserManager.Update(student);

    //code omitted for brevity              
}

When using this method, I encounter an error: "The type arguments for method 'UserManagerExtensions.Update(UserManager, TUser)' cannot be inferred from the usage. Try specifying the type arguments explicitly." Any idea to fix it?

Amphibolite answered 17/9, 2016 at 0:4 Comment(6)
The problem is not automapper, but your inheritance between Student and ÀpplicationUser. Why don't you just create a 1-1 relationship between these 2 if you don't want to put everything in ApplicationUser?Rackrent
Do you meant that I should inherit from IdentityUser instead of ApplicationUser in Student class (public class Student: IdentityUser {...})? On the other hand, I have a look at Maping Inheritance page many times, but could not convert the first example usage for my situation. Could you also modify that sample according to my classes above? Many thanks...Amphibolite
As I said, the problem isn't AutoMapper... You have a UserManager<ApplicationUser>. The error tells you it's can't Update a Student.Rackrent
@GabrielGM What is the solution? Could you please post the solution?Amphibolite
See this question to make a 1-1 relationship between ApplicationUser and another table.Rackrent
+1 for adding ForAllOtherMembers() to ignore every field instead of manually ignoring one by one. Allowed me to remove a TON of ignore code. :) Thank you.Unrest

© 2022 - 2024 — McMap. All rights reserved.