简体   繁体   English

创建新的角色提供程序时如何停止此错误?

[英]How do I stop this error when creating a new Role Provider?

I'm trying to add a new Role Provider into my MVC5 project - however on the "return roles.ToArray();" 我试图在我的MVC5项目中添加一个新的角色提供程序-但是在“返回角色.ToArray();”上 line I get the error: 行我得到错误:

Cannot implicitly convert type 'wb.Models.UserRole[]' to 'string[]'

    public override string[] GetRolesForUser(string username)
    {
        var roles = db.UserRole
                    .Where(x => x.UserName
                    .Equals(username, StringComparison.CurrentCultureIgnoreCase));

        if (roles != null)
                return roles.ToArray();
            else
               return new string[] { }; ;
     }

Can anyone suggest how I correct this please? 有人可以建议我如何纠正此问题吗?

You are returning an array of roles, not role names. 您将返回一组角色,而不是角色名称。 Select out the names and return them as an array. 选择名称并将它们作为数组返回。

public override string[] GetRolesForUser(string username)
{
    var roleNames = 
        db.UserRole
            .Where(x => x.UserName.Equals(username, 
                StringComparison.CurrentCultureIgnoreCase))
            .Select(x => x.RoleName);

    if (roleNames.Count() > 0)
        return roleNames.ToArray();
    else
        return new string[] { }; ;
 }

The problem in your code is in the following line: 您的代码中的问题在以下行中:

roles.ToArray();

This returns an array of wb.Models.UserRole instead of an array of strings, string[] . 这将返回wb.Models.UserRole数组,而不是字符串数组string[] One option I see, it would be before the call of ToArray() to select an anonymous type with one string property, which would contain the information you want. 我看到的一个选项是,在调用ToArray()之前选择具有一个字符串属性的匿名类型,该属性将包含所需的信息。 Something like this: 像这样:

 var roles = db.UserRole
               .Where(x => x.UserName
               .Equals(username, StringComparison.CurrentCultureIgnoreCase))
               .Select(x => x.RoleName);

I am not sure if your class has a property called RoleName , but in general terms you should project the results of your query like above, in a sequence of strings. 我不确定您的类是否具有一个名为RoleName的属性,但总的来说,您应该按照一系列字符串的方式投影查询的结果。 Then calling ToArray will create an array of strings and you would be more than done. 然后调用ToArray将创建一个字符串数组,您会做的不止这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DbContext - 如何在更新数据库时阻止数据库创建新记录? - DbContext - how do I stop my database from creating new records when I'm updating them? 如何在创建新类时传递类的实例? - How do I pass an instance of a class when creating a new class? 如何在创建新文件夹时复制安全信息? - How do I copy security information when creating a new folder? 创建新用户时添加角色 - Adding a role when creating a new user 包含导航属性时,如何阻止 Entity Framework Core 创建“自引用循环”? - How do I stop Entity Framework Core from creating “Self Referencing Loops” when including navigation properties? 当我输入“new {”时,如何阻止Visual Studio插入“object” - How do I stop Visual Studio from inserting “object” when I type “new {” 创建新实体时如何验证加载的表单? - How do I validate a form on load when I am creating a new entity? 您如何实现ASP.NET角色提供程序? - How do you implement an ASP.NET role provider? 如何阻止游戏对象创建副本 - How do I stop gameobject from creating a copy 添加新角色时可以设置角色描述吗 - can i set a role description when adding a new role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM