1

I use ASP.NET Identity 2.0 and create two different user entities inherited from ApplicationUser as posted on Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 but I cannot reach these custom properties and its values (StudentNumber and Profession) while reaching custom properties defined in ApplicationUser (Name, Surname). And idea?

Here is the code I tried to use for retrieving value Name, Surname, StudentNumber and Profession:

Controller:

public AccountController(ApplicationUserManager userManager, 
    ApplicationSignInManager signInManager)
{
    UserManager = userManager;
    SignInManager = signInManager;
}

private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
    get
    {
        return _userManager ?? HttpContext.GetOwinContext()
            .GetUserManager<ApplicationUserManager>();
    }
    private set
    {
        _userManager = value;
    }
}


[AllowAnonymous]
public ActionResult ListAccount(JQueryDataTableParamModel param)
{
    var allUsers = UserManager.Users;
    //code omitted for brevity

    /* as there is no property StudentNumber and Profession in Users, 
    I cannot reach these properties while reaching other custom properties */ 
    var result = from c in allUsers 
select new[] { Convert.ToString(c.Id), c.Name, c.Surname, c.Email, c.PhoneNumber };
 }

Models:

public abstract class ApplicationUser : IdentityUser
{
    //snip
    //Common properties
    public string Name { get; set; }
    public string Surname { get; set; }
}

I defined different type classes and inherited from the base class:

public class StudentUser : ApplicationUser
{
    public string StudentNumber { get; set; }
}

public class ProfessorUser : ApplicationUser
{
    public string Profession { get; set; }
}
Jack
  • 1
  • 21
  • 118
  • 236
  • @Stephen Muecke Thanks for editing. Any idea to fix that problem? – Jack Aug 27 '16 at 08:44
  • Its not clear what your problem is - You have said _Here is the code I tried to use for retrieving value_ but you have not shown any code for getting a value (just some code for initializing a new `ApplicationUser` and setting properties) –  Aug 27 '16 at 08:49
  • @StephenMuecke DO you have any idea regarding to this serious problem? Sorry, but I have not found any fix yet although looking at many web pages :( – Jack Aug 27 '16 at 09:26
  • @binary I don't get it, where are you trying to access it? are you looking for the fields in the database? If you're using code first then you need to add the migration. If you are trying to access it as a claim, then you need to add the claim. if you are trying to access it from a controller then you need to cast IPrinciple as ClaimsIdentity and then get it from claim. More details would be good. – Joe_DM Aug 27 '16 at 11:52
  • in line "var allUsers = UserManager.Users;" what type is var? – Joe_DM Aug 27 '16 at 11:55
  • I use code first and it is ok for me to access it by adding an entity to DbContext as the other entities. On the other hand regarding to your suggestions (Claims) there is an answer on [this](http://stackoverflow.com/questions/38846816/how-to-get-custom-property-value-of-the-applicationuser-in-the-asp-net-mvc-5-vie) page, but I am not sure if it is good idea. For the other issue, var is already a variable type, but you can also create other type i.e. ApplicationUser. In here I just want to retrieve data from AspNetUsers table and there might be different approaches to do this. Thanks... – Jack Aug 27 '16 at 12:30

1 Answers1

1

When creating a list of variables created from varying classes from a base class you are only going to be able to access the attributes from the base class.

If you create a list of one object type only, then yes you can access the added attributes of that inherited class e.g. for StudentUser, you will be

List<StudentUser> students, you will be able to access the StudentNumbers from these items.

There's not much you can do if you are wanting to grab a list from the base class to catch all Users. You can only get the attributes from that list that are common to all classes in the list.

For populating a view with varying classes, some condition testing for class type would do to populate the attribute specific to that class.

For e.g.

if(usertype == Student)
    room = user.Room;
  • I list all the users on a grid and there are also 2 columns called StudentNumber and Rooms. So, I need to retrieve values from three of the entities (ApplicationUser, StudentUser and ProfessorUser). For this reason, getting data from one or two entity will not be enough for me. On the other hand, I am wondering if I can solve this issue using a ViewModel? I already have a ViewModel containing the necessary properties from all of three entities, but I have not any idea how to inject the Users to the ViewModel (because there is no property of Room in the User entity). Any idea? – Jack Aug 27 '16 at 13:12
  • 1
    Ok, thanks. I will try to find an approach and then apply to my problem. Later I will post a specific question and as you said by making this we will have a concrete problem to solve. Regards. – Jack Aug 27 '16 at 14:51