Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 扩展IdentityUser和自定义查询的Web API_Entity Framework_Asp.net Web Api_Ef Code First_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

Entity framework 扩展IdentityUser和自定义查询的Web API

Entity framework 扩展IdentityUser和自定义查询的Web API,entity-framework,asp.net-web-api,ef-code-first,asp.net-mvc-5,asp.net-identity,Entity Framework,Asp.net Web Api,Ef Code First,Asp.net Mvc 5,Asp.net Identity,在Web API的AccountController中,我可以访问UserManager,它提供了GetUserById()等方法。我需要根据扩展的属性查询上下文并搜索用户列表,例如,我需要添加一个“GetUsersByPhoneNumber()”方法 AppUser是我的扩展类,它继承IdentityUser 我的构造函数是这样的。我在网上找到的任何例子都显示了与传入的上下文不同的内容。我看不到在哪里可以访问dbcontext public AccountController()

在Web API的AccountController中,我可以访问UserManager,它提供了GetUserById()等方法。我需要根据扩展的属性查询上下文并搜索用户列表,例如,我需要添加一个“GetUsersByPhoneNumber()”方法

AppUser是我的扩展类,它继承IdentityUser

我的构造函数是这样的。我在网上找到的任何例子都显示了与传入的上下文不同的内容。我看不到在哪里可以访问dbcontext

    public AccountController()
        : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
    {
    }

    public AccountController(UserManager<AppUser> userManager,
        ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
    {
        UserManager = userManager;
        AccessTokenFormat = accessTokenFormat;
    }
publiccountcontroller()
:这(Startup.UserManagerFactory()、Startup.OAuthOptions.AccessTokenFormat)
{
}
公共帐户控制器(用户管理器用户管理器,
ISecureDataFormat访问令牌格式)
{
UserManager=UserManager;
AccessTokenFormat=AccessTokenFormat;
}

如何访问dbcontext并根据我添加到AppUser类中的额外字段执行自己的查询?

UserManager意味着对存储不可知,如果您挂起UserManager构建时使用的UserStore本身,您可以从dbcontext本身下拉并访问内容

由于工厂创建了用户管理器,您可能需要将UserManager子类化,以公开对默认受保护的IUserStore的访问。一旦拥有了存储,就可以将其强制转换为UserStore并直接访问DbContext

另一种可能更简洁的方法是扩展UserManager并从UserManager本身公开一个新的GetUsersByPhoneNumber(),因为它已经可以访问您需要的所有内容