在asp.net核心Web应用程序2.1中搭建管理员CRUD

时间:2018-12-22 21:29:46

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

我想在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操作时,在模型类中列出了投资者模型。我用了它,但是当我运行我的项目时,我看不到已经注册用户的列表。

这是我创建的Application User类。

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剃须刀”页面上未列出),因为它是我在创建用户时使用的类。

1 个答案:

答案 0 :(得分:1)

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

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

对于ApplicationUser继承的IdentityUser,用户将包含许多属性,例如ConcurrencyStampSecurityStamp。如果您直接通过ControllerDbContext创建用户,则可能会错过为其设置值的可能性,这将导致Identity Feature不稳定。

无需使用ApplicationUser创建新的Controller,可以使用AccountControllerManageController来控制ApplicationUser,早于Asp.Net Core 2.1。对于Asp.Net Core 2.1及更高版本,您可以尝试Identity Scaffold自定义Identity

对于带有ApplicationUser的{​​{1}},您可以通过注入CRUD来创建控制器。

相关问题