How can I retrieve the data of registered users?

 public ActionResult Restaurants()
        {
            var restaurants = _context.ApplicationUsers.ToList();
            return View(restaurants);
        }

      

I want to extract information about registered users from ASP.NET MVC and list it. But when I have executed above the code, I get the error:

Multiple sets of objects for each type are not supported. The ApplicationUsers and Users object sets can contain instances of type 'ESportsScreening.Models.ApplicationUser'.

How can I list the information of registered users in ASP.NET MVC 5?

+3


source to share


2 answers


ASP.NET does not allow you to have 2 DbSet<T>

in the same context with the same entity type. The most likely reason for this error is initializing yours DbContext

like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
}

      



The above code ApplicationDbContext

already contains a DbSet

type ApplicationUser

inherited from IdentityDbContext<ApplicationUser>

(which you can access via _context.Users

). By manually adding the second, DbSet<ApplicationUser>

we create 2 sets with the same entity type. Remove the manual property DbSet

and use the inherited property instead Users

.

+2


source


The error indicates that you have 2 sets of objects of the same type ESportsScreening.Models.ApplicationUser. You must remove one of them in order to resolve this error. You should only have one set of objects for each type and you don't need it anymore.

To find out how you can find registered users, you need to provide some information about what you consider registered users. I think that all the users you have in your database are logged in users, in which case the following line of code is sufficient:

var restaurants = _context.ApplicationUsers.ToList ();



If you need to filter users, you can use the Where extension method like this:

var restaurants = _context.ApplicationUsers.Where (x => yourconditionhere) .ToList ();

0


source







All Articles