1

I am using Entity Framework 6 Code First. I created a separate table to contain the UserId from the aspnet_Users table and a field for Department. So the user listed with the departments have only access to those departments.

Since all of my table are generated via code first and is using the db.context. The membership and roles are pre-generated table from MVC. How to I get a list of UserId from the aspnet_Users table when it's not in the db.context? The aspnet tables are pre-generated via a script in the entity framework.

How do I query tables in MVC outside of my db.context?

3 Answers 3

1

Your DbContext will have a Database property. Off of that you will find two overloads for general SQL queries:

DbRawSqlQuery    SqlQuery   (Type elementType, string sql, params object[] parameters);
DbRawSqlQuery<T> SqlQuery<T>(string sql, params object[] parameters);

For example:

var result = ctx.Database.SqlQuery<Foo>("select * from foo");

More information here

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

2 Comments

var users = context.Database.SqlQuery<SelectListItem>("Select UserId, UserName from aspnet_users"); What exactly do I put in the "Foo" part? if I want to assign this to the ViewBag for a @Html.DropDownList. The above line gives me null....
Thanks @Amy for directly answering the question. Gave me the clue I needed for a solution. Added my complete solution for other beginner's like myself.
1

Since you are using .Net Membership, you could always call the

MembershipProvider.GetAllUsers(int pageIndex, int pageSize, out int totalRecords)

Then from that result, generate a list of UserIds. The role provider also offers similar functionality with a Roles.GetAllRoles() method.

The membership provider and role provider offer many more useful methods to hopefully get you the data you are looking for. If they still don't have what you are after you have a couple of more options. You can use your db context to execute raw SQL. See MSDN for more info.

Another option is to create additional entity classes that match the DB structure of those tables, and add them to your DB Context. The downside to this approach is it could allow another developer, or even yourself to create and update users and roles without going through the proper providers, in which case you would lose some functionality. If that is a concern, you could always create a DB View and map to that to ensure read only access. It's a bit more overhead, but does give you type safety and a familiar way to query the data.

1 Comment

using the membership GetAllUsers returns 0. I think because I am using the SqlRoleManager in a windows authentication environment. Not sure, but going with @amy method
0

Here is what I did to get the complete solution I wanted:

To query the data:

 string sql = "Select UserId, UserName from aspnet_users where applicationid='" + Config.Instance.AppId() + "'";

 using (var context = new NameSystemContext())
 {
      var users = context.Database.SqlQuery<AspnetUser>(sql);
      ViewBag.UserId = new SelectList(users, "UserId", "UserName").ToList();
 }

In your view models or somewhere define a class:

class AspnetUser
{


    [Key]
    //[Column(Order = 1)]
    public Guid UserId { get; set; }

    //[Key]
    //[Column(Order = 2)]
    public string UserName { get; set; }


}

//ViewBag.UserId could be use in the view

@Html.DropDownList("UserId", null, "Select...", htmlAttributes: new { @class = "form-control" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.