无法检索AspNetUsers表继承自定义属性值 [英] Cannot retrieve inherited custom property value from AspNetUsers table

查看:152
本文介绍了无法检索AspNetUsers表继承自定义属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET 2.0的身份,并创建两个不同的用户从实体继承ApplicationUser上的实现实体框架6在ASP.NET MVC继承5 但我不能达到这些自定义属性和它的值(StudentNumber和职业),而在达到ApplicationUser(名字,姓氏)定义的自定义属性。和想法?



下面是我试图用检索值的代码名称 StudentNumber 行业



控制器:

 公开的AccountController(ApplicationUserManager的UserManager,
ApplicationSignInManager signInManager)
{
的UserManager =的UserManager;
SignInManager = signInManager;
}

私人ApplicationUserManager _userManager;
公共ApplicationUserManager的UserManager
{
得到
{
返回_userManager? HttpContext.GetOwinContext()
.GetUserManager< ApplicationUserManager>();
}
私定
{
_userManager =价值;
}
}


[使用AllowAnonymous]
公众的ActionResult ListAccount(JQueryDataTableParamModel参数)
{
VAR ALLUSERS =的UserManager。用户;不再赘述


//代码/ *因为没有财产StudentNumber和职业中的用户,
同时达到其他自定义属性我不能达到这些特性* /
VAR的结果=从C在ALLUSERS
选择新的[] {Convert.ToString(c.Id),c.Name,c.Surname,c.Email,c.PhoneNumber};
}



型号:



 公共抽象类ApplicationUser:IdentityUser 
{
//剪断
//公共属性
公共字符串名称{搞定;组; }
公共字符串姓氏{搞定;组; }
}



我定义的不同类型的类并从基类继承:

 公共类StudentUser:ApplicationUser 
{
公共字符串StudentNumber {搞定;组; }
}

公共类ProfessorUser:ApplicationUser
{
公共字符串行业{搞定;组; }
}


解决方案

在创建列表从基类你只打算要能够从基类访问属性不同类创建变量。



如果你创建一个对象类型的列表,只,然后是你可以访问继承类如的添加的属性为StudentUser,你会



列表< StudentUser>学生,你将能够从这些项目获得的StudentNumbers。



有没有多少,如果你是想抓住一个列表,你可以做从基类来捕获所有用户。你只能得到从该列表中是常见的列表中的所有类的属性。



有关填充有不同类的视图,为类类型一定条件下的测试会做来填充特定于类的属性。



有关如

 如果(用户类型==学生)
室= user.Room;


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; }
}

解决方案

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;

这篇关于无法检索AspNetUsers表继承自定义属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆