AspNetUserRoles table not populated using Entity Framework database First in MVC 5

In my project, I try to find all users and their roles in MVC 5 using the Entity Framework.

DBContext is first created using the database. But the fatigue is that the AspNetUserRoles table does not automatically populate, and the relationship between AspNetUser and AspNetRole is many-to-many.

Below is a link to a screenshot.

image

Can someone give me some hints of this? Many thanks.

+4
source share
3 answers

AspNetUsersRoles, .

  • , UserId RoleId

  • .

  • .

enter image description here

.

. , , , , AspNetUserRoles , , , : MyRoles = 1 (PK), UserID = 1, RoleID = 1, Comment = User1 - Admin MyRoles = 2 (PK), UserID = 1, RoleID = 2, = User1 -

, AspNetUserRoles!

HappyCoding

+5

, , AspNetUserRoles EDMX .

.

ASP.NET MVC 5 -

   public class AccountController : Controller
    {
        private ApplicationUserManager _userManager;
 ...
 public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
        {

            UserManager = userManager;
...
 public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }

, Login ,

  [System.Web.Mvc.HttpPost]
        [System.Web.Mvc.AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
...
 var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, false, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    {     
                        ApplicationUser user = UserManager.FindByName(model.Email);

                        var roles = (AppUserRoles[])Enum.GetValues(typeof(AppUserRoles));
                        foreach (var role in roles)
                        {
                            if (UserManager.IsInRole(user.Id, role.ToString()) && role == AppUserRoles.Administrators)
                            {
                                 // Do what you need here                                

                                // logger.Info("Login attempt is success.");

                                return RedirectToAction("Index", "Admin");
                            }

public enum AppUserRoles
    {
        Administrators = 1,
        Managers = 2,
        Users = 3,
        Others = 4
    }

- , EDMX.

CREATE PROCEDURE GetUserRoleByUserId
    @UserId nvarchar(128) 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT [UserId],[RoleId] FROM [AspNetUserRoles] WHERE [UserId] LIKE @UserId
END
GO

, .

+2

Source: https://habr.com/ru/post/1541861/


All Articles