不包含'users'的定义,也不包含'Users'接受第一个参数类型的扩展方法 [英] Does n't contain a definition for 'users' and no extension method 'Users' accepting a first argument type

查看:79
本文介绍了不包含'users'的定义,也不包含'Users'接受第一个参数类型的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有



我正在尝试为用户分配角色,但我收到以下错误,我的代码如下。我正在使用Framework Version 4.5



指令: -

-------------



Dear All

I am trying to Assign roles to users but I am getting the following error my code is as follows. I am using Framework Version 4.5

Directives:-
-------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Web.Security;







方法

------








Method
------


private void GetAllUsers()
      {
          var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
          var users = userManager.Users.ToList();  // This Line Containing the error//
          ddlUsers.DataTextField = "UserName";
          ddlUsers.DataValueField = "Id";
          ddlUsers.DataSource = users;
          ddlUsers.DataBind();
      }







Web.Config

--- --------








Web.Config
-----------


<membership>

     <providers>

       <!--

          ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template

       -->

       <clear />

     </providers>

   </membership>







<profile>
     <providers>
       <!--
       ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
       -->
       <clear/>
     </providers>
   </profile>










<roleManager>

     <!--

            ASP.NET Membership Role is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template

       -->

     <providers>

       <clear />

     </providers>

   </roleManager>

推荐答案

userManager是UserManager类的一个实例,而不是UserStore - 所以你需要得到商店才能获得用户。试试这个:

userManager is an instance of the UserManager class, not UserStore - so you need to get the store before you can get the users. Try this:
var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
var users = userManager.Store.Users.ToList();  // This Line Containing the error//



但是请不要使用 var 懒得 - 它鼓励这种问题,因为你不一定知道它是什么样的变量。

如果您使用过实际类型:


But please, don't use var so lazily - it encourages this kind of problem because you don't necessarily realise what kind of variable it is.
If you had used the actual type:

UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());

很明显它不是你正在处理的UserStore实例。

It would have been obvious that it wasn't a UserStore instance you were dealing with.


这篇关于不包含'users'的定义,也不包含'Users'接受第一个参数类型的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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