Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 5 Identity userManager.IsInRole_C#_Asp.net_Asp.net Mvc_Entity Framework Migrations_Asp.net Identity - Fatal编程技术网

C# ASP.NET MVC 5 Identity userManager.IsInRole

C# ASP.NET MVC 5 Identity userManager.IsInRole,c#,asp.net,asp.net-mvc,entity-framework-migrations,asp.net-identity,C#,Asp.net,Asp.net Mvc,Entity Framework Migrations,Asp.net Identity,下面的代码不起作用,我无法解释为什么。。。我的用户管理器造成了很大的麻烦,因为它创建的用户和角色很好,但当我运行此代码时,userManager.IsInRole总是返回false,因此第二次运行种子时,我遇到了错误,因为它试图创建记录,尽管它已经存在 请注意,这是在我针对迁移项目运行更新数据库时发生的,这是一个非ASP项目造成的问题吗?如果是,为什么?不应该抛出错误 这是我使用Identity的第一个项目,虽然它工作起来似乎很好,但几乎没有最新的高质量文档可用,因此如果有人对此有任何来源,我将

下面的代码不起作用,我无法解释为什么。。。我的用户管理器造成了很大的麻烦,因为它创建的用户和角色很好,但当我运行此代码时,userManager.IsInRole总是返回false,因此第二次运行种子时,我遇到了错误,因为它试图创建记录,尽管它已经存在

请注意,这是在我针对迁移项目运行更新数据库时发生的,这是一个非ASP项目造成的问题吗?如果是,为什么?不应该抛出错误

这是我使用Identity的第一个项目,虽然它工作起来似乎很好,但几乎没有最新的高质量文档可用,因此如果有人对此有任何来源,我将不胜感激

    public void Run(BlogContext blogContext)
    {
        var userStore = new UserStore<User>((BlogContext) blogContext);
        var userManager = new UserManager<User>(userStore);

        var userRoles = new List<UserRole>()
        {
            new UserRole() {Username = "SysAdmin@test.com", Role = "SysAdmin"},
            new UserRole() {Username = "testAdmin@test.com", Role = "Admin"},
            new UserRole() {Username = "testAuthor@test.com", Role = "Author"}
        };

        foreach (var userRole in userRoles)
        {
            var userId = userManager.FindByName(userRole.Username).Id;

            if (!userManager.IsInRole(userId, userRole.Role))
                userManager.AddToRole(userId, userRole.Role);
        }


        blogContext.SaveChanges();
    }
public void运行(BlogContext-BlogContext)
{
var userStore=newuserstore((BlogContext)BlogContext);
var userManager=newusermanager(userStore);
var userRoles=new List()
{
新用户角色(){Username=”SysAdmin@test.com,Role=“SysAdmin”},
新用户角色(){Username=”testAdmin@test.com,Role=“Admin”},
新用户角色(){Username=”testAuthor@test.com,Role=“Author”}
};
foreach(userRoles中的var userRole)
{
var userId=userManager.FindByName(userRole.Username).Id;
if(!userManager.IsInRole(userId,userRole.Role))
AddToRole(userId,userRole.Role);
}
blogContext.SaveChanges();
}

因此,我将亲自回答这一问题,以避免任何人因此而遭受数小时的痛苦

发生这种情况的原因是我禁用了延迟加载,我已经在迁移项目中启用了这种功能

protected override void Seed(BlogContext blogContext)
    {
        AutomaticMigrationsEnabled = true;
        blogContext.Configuration.LazyLoadingEnabled = true;
        //Add seed classes here!    
    }