【问题标题】:scaffold an admin CRUD in asp.net core web application 2.1在 asp.net core web application 2.1 中搭建一个管理员 CRUD
【发布时间】:2018-12-22 21:29:46
【问题描述】:

我想在 asp.net 核心中搭建一个 CRUD 管理员。但应用程序用户类不包括在列出的模型类中。

我试图创建另一个不从身份用户继承的类,

public class Investors
{

    public string FullName { get; set; }

    public string Country { get; set; }

    public string Project { get; set; }

    public string Telephone { get; set; }

    }    

当我尝试构建 CRUD 操作时,投资人模型被列在模型类中。我使用了它,但是当我运行我的项目时,我看不到已注册用户的列表。

这是我创建的应用程序用户类。

public class ApplicationUser : IdentityUser
{

    public string FullName { get; set; }

    public string Country { get; set; }

    public string Project { get; set; }

    public string Telephone { get; set; }

将返回已注册用户列表的实际模型类将是应用程序用户类(但在脚手架 CRUD 剃须刀页面时未列出),因为它是我在创建用户时使用的。

【问题讨论】:

  • 您在搭建模型之前构建项目吗?这是强制性的(在每次更改模型之后也是如此),因为模型列表取自 compiled dll
  • 是的,我愿意。但仍然没有显示应用程序用户模型

标签: asp.net asp.net-mvc asp.net-core asp.net-core-2.1


【解决方案1】:

作为我的测试,我可以得到相同的结果。它似乎与为Identity 定义的Identity classes 相关。

IMO,您应该避免直接从控制器通过DbContext 操作ApplicationUser

对于继承自IdentityUserApplicationUser,用户将包含许多属性,例如ConcurrencyStampSecurityStamp。如果您通过DbContext 直接从Controller 创建用户,您可能会错过为它们设置值,这将导致您的Identity Feature 不稳定。

没有必要用ApplicationUser创建一个新的Controller,你可以使用AccountControllerManageControllerAsp.Net Core 2.1之前控制ApplicationUser。对于Asp.Net Core 2.1 及更高版本,您可以尝试Identity Scaffold 自定义Identity

对于ApplicationUserCRUD,您可以通过注入UserManager<ApplicationUser> 创建控制器。

【讨论】:

  • 如果不以某种方式扩展UserManager,我真的不认为这是一个选项IF您还使用自己的属性扩展了ApplicationUser(就像我的情况一样.
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 2023-03-09
  • 2018-01-08
  • 1970-01-01
  • 2021-05-01
  • 2023-03-15
  • 2019-02-19
  • 1970-01-01
相关资源
最近更新 更多