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; }
}
6
  • @Stephen Muecke Thanks for editing. Any idea to fix that problem? Commented Aug 27, 2016 at 8: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) Commented Aug 27, 2016 at 8: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 :( Commented Aug 27, 2016 at 9: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. Commented Aug 27, 2016 at 11:52
  • in line "var allUsers = UserManager.Users;" what type is var? Commented Aug 27, 2016 at 11:55

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.