本文介绍了无法投类型的对象System.Web.Security.SqlRoleProvider为键入'WebMatrix.WebData.SimpleRoleProvider“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展与实体框]工作MVC的Web应用程序。我已经启用数据库迁移,这样我可以添加每次更新一些种子数据。
更具体地讲,我想添加两个用户和两个角色;所以配置文件看起来像这样:

I developing an mvc web app with Entity Frame]work. I've enabled database migration so that i can add some seed data on each update. More specifically, i want to add two users and two roles; so the configuration file looks like this:

        var roles = (SimpleRoleProvider)Roles.Provider;
        var membership = (SimpleMembershipProvider)Membership.Provider;

        //// create two roles 
        if (!roles.RoleExists("Admin"))
        {
            roles.CreateRole("Admin");
        }
        if (!roles.RoleExists("User"))
        {
            roles.CreateRole("User");
        }

但是似乎有铸造过程中的问题;它抛出一个异常

However there seems to be a problem during the casting; it throws an exception

 Unable to cast object of type 'System.Web.Security.SqlRoleProvider' to type 'WebMatrix.WebData.SimpleRoleProvider'.

我怀疑这可能是一个配置问题,但我真的不知道。是否在相同问题的人绊倒?

I suspect that this might be a configuration issue, but i'm not really sure. Does anyone stumbled across the same problem?

推荐答案

我整理了这一点。这个问题显然是有关网络配置。我添加了以下行的web.config文件:

I sorted this out. The problem apparently was related to web configuration. I added the following lines to the web.config file:

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
  <providers>
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
  </providers>
</roleManager>

要明确设置角色提供。所以现在的 Roles.Provider 返回WebMatrix.WebData.SimpleRoleProvider的实例;因此,我并不需要施放任何更多

to explicitly set the role provider. So now the Roles.Provider returns an instance of WebMatrix.WebData.SimpleRoleProvider; thus i don't need to cast any more

这篇关于无法投类型的对象System.Web.Security.SqlRoleProvider为键入'WebMatrix.WebData.SimpleRoleProvider“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 00:23