【发布时间】:2015-10-25 00:20:27
【问题描述】:
我正在尝试使用 ASP.NET MVC 角色。我在 web.config 中添加了一个条目:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\resorg-db.mdf;Integrated Security=True"/>
</connectionStrings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<roleManager enabled="true" />
</system.web>
我没有启用迁移,现在我更喜欢在每次模型更改时重新创建数据库。
我以编程方式在 DB Initialize 方法中添加一个角色,例如:
Roles.CreateRole("root");
Roles.AddUserToRole(testUser.UserName, "root");
context.SaveChanges();
问题: 当我第一次运行它时,一切都很好。当我更改模型并再次运行初始化方法时,我收到角色已存在的错误。 我使用服务器资源管理器查看了数据库,但 AspNetRoles 和 AspNetUserRoles 都是空的。 角色保存在哪里?在与我的数据库相同的文件夹中,还有另一个数据库,名为 aspnetdb.mdf。
我尝试使用 SysTools MDF Viewer 1.0 打开它,但格式无效。
额外问题:我还在另一个项目中进行了一些测试。这个重新创建数据库并运行相同的测试。我的问题是我收到了这个错误:
System.Configuration.Provider.ProviderException: The Role Manager feature has not been enabled.
虽然我在我的 app.config 中为 unittest 项目添加了相同的条目。
【问题讨论】:
-
它在
AspNetRoles中挑衅,尝试刷新表格内容,你说你创建了2个DB,你自己创建了一个吗? -
发现问题:我需要将连接字符串添加到roleManager条目中。如果没有连接字符串,ASP.net 将创建一个不同的数据库来存储角色。见stackoverflow.com/questions/8911738/…
标签: c# asp.net asp.net-mvc asp.net-mvc-4