0

In an MVC application, there is 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?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Jack
  • 1
  • 21
  • 118
  • 236

1 Answers1

1

The error you are getting has nothing to do with AutoMapper.

The problem is that your student variable is of type object due to the following line

var student = (Object)null;

while it should be Student.

Either remove the above line and use

var student = Mapper.Map<Student>(model);

or change it to

Student student = null;
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Many thanks for your reply. I tried to use **Student student = null;** but in that case student properties are null after **student = Mapper.Map(model);** line. Is there any mistake? On the other hand, as I use inheritance, might there be another solution using mapping of base/inherited class in Automapper? – Jack Sep 17 '16 at 07:52
  • The result of `Mapper.Map` is not related to the type of the receiving variable. Now when your code compiles, you seem to have a mapping issue. I would check `.ForAllOtherMembers(opts => opts.Ignore())` call - it sounds to me that you are ignoring (not mapping) all the members of the `StudentViewModel`, consider removing that call. – Ivan Stoev Sep 17 '16 at 08:00
  • Yes, you are right. I ignored the related properties indicated on "Unmapped members were found." error. However, although student variable is properly populated with the new data, **UserManager.Update(student)** cannot update the student even if there is no error. I also tried to use **ApplicationUser** insted of Student class as it inherited from ApplicationUser but it did not make any sense and the record is not updated. Any idea? – Jack Sep 17 '16 at 08:26
  • What about solving the problem without AutoMapper? Could you please have a look at [Updating user by UserManager.Update() in ASP.NET Identity 2](http://stackoverflow.com/questions/39545037/updating-user-by-usermanager-update-in-asp-net-identity-2)? – Jack Sep 17 '16 at 09:18
  • Do you have any idea? – Jack Sep 17 '16 at 09:45