无法检索AspNetUsers表继承自定义属性值

编程入门 行业动态 更新时间:2024-10-22 07:31:35
本文介绍了无法检索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表继承自定义属性值

本文发布于:2023-10-27 03:07:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性   AspNetUsers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!