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?

Jack
  • 1
  • 21
  • 118
  • 236
  • 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`? – Gabriel GM Sep 17 '16 at 00:17
  • 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](https://github.com/AutoMapper/AutoMapper/wiki/Mapping-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... – Jack Sep 17 '16 at 00:28
  • As I said, the problem isn't AutoMapper... You have a `UserManager`. The error tells you it's can't `Update` a `Student`. – Gabriel GM Sep 17 '16 at 01:21
  • @GabrielGM What is the solution? Could you please post the solution? – Jack Sep 17 '16 at 07:18
  • See [this question](http://stackoverflow.com/questions/25884399/aspnetusers-id-as-foreign-key-in-seperate-table-one-to-one-relationship) to make a 1-1 relationship between `ApplicationUser` and another table. – Gabriel GM Sep 17 '16 at 11:33
  • +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. – Erick Brown Oct 12 '17 at 16:37

0 Answers0