3

I am trying to query the AspNetUsers table.

I have set up the db variable as follows

public ApplicationDbContext db = new ApplicationDbContext(); 

However, the intellisense for db does not list any of the Identity tables, but it does the others created for the web site (which are listed in IdenityModels.cs) e.g .......

public System.Data.Entity.DbSet<FeatureRequestMVC.Models.Feature> Features { get; set; } 

How can I get the Identity tables (like AspNetUsers) listed in the db intellisense ?

Thanks

5
  • Normaly you can use WebMatrix.WebData.WebSecurity to get identities related functions... you can also manually add those ASpNetUsers table into your ApplicationDbContext class. Commented May 29, 2015 at 13:24
  • @nik0lias Thanks for your reply, I did look at this but then saw this post reply who said not to do that... stackoverflow.com/questions/22141610 ... but also I did try it by adding public virtual DbSet<AspNetRole> AspNetRoles { get; set; } to IdentityModels.cs, but it says that AspNetRole cannot be found. Commented May 29, 2015 at 13:51
  • @RosdiKasim WebMatrix is old stuff. This question is about new Identity framework. Commented May 29, 2015 at 14:43
  • If it helps, I can't access GetUser either when using User.Identity.GetUser() .. it might be related. Commented May 29, 2015 at 14:52
  • I wasn't able to use User.Identity.GetUser() because I hadn't included using Microsoft.AspNet.Identity;. Commented Jun 2, 2015 at 6:58

2 Answers 2

2

You can use the following code to instantiate the UserManager Class,

UserManager<ApplicationUser> userManager; userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 

Once you have added all the using statements required, you can call all sorts of methods to deal with Identity users.

// find user by id, also available for email and username var user = await userManager.FindByIdAsync(Id param); // with the user object above you can delete the referenced user await userManager.DeleteAsync(user); // Update user await userManager.UpdateAsync(user); 

All methods above use the async and await keyword

MSDN UserManager

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, using the UserManager helped. I was then able to loop around the users this way. I had just assumed that I should be able to use the AspNetUsers etc, tables in my Linq.
0

The identity wants you to go through the UserManager for data, however there are cases where you need to query the identity tables. You can do something like

var sqlQuery = @"SELECT Users.UserName FROM AspNetUserClaims AS Claims INNER JOIN AspNetUsers AS Users ON Claims.UserId = Users.Id WHERE Claims.ClaimType = {0} AND Claims.ClaimValue = {1}"; var userName = dbContext.Database.SqlQuery<string>(sqlQuery, "MyClaimType", theClaimValue).ToList().FirstOrDefault(); 

This is pulling a single UserName and setting the variable as a string. You can create a model and pass that model into SqlQuery<T> as long as the property names match the column or alias names in your query.

2 Comments

Thank you for your response. I was able to use UserManager to resolve most of my issues. Does this mean that you can't access the AspNetUsers table through Linq but have to use the technique you described? I still have a couple of outstanding issues because I've got a Linq query that uses the UserManager within it but is complaining about there being more than one context but am going to ask about that in a seperate question.
It depends on the avenue you want to go. Not sure if there is a way to query every user with UserManager, which is why I used SqlQuery<T>. There really is no right or wrong answer, albeit UserManager makes it easier to get a single users details but for running a report, like getting all users that registered but didn't finalize their registration, you can use something like I mentioned. I pass a string for T, but you could pass in a model, as long as the column name/alias match the property of the model so it can map to it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.