我正在尝试将用户和角色植入我的数据库中。
目前在 C# MVC4 中使用具有自动迁移功能的 Code First 实体框架。
每当我打电话
更新数据库-强制
我收到以下错误:
运行种子方法。
System.InvalidOperationException:您必须调用
调用任何方法之前的“WebSecurity.InitializeDatabaseConnection”方法
“WebSecurity”类的其他方法。这个调用应该放在
站点根目录中的 _AppStart.cshtml 文件。
在 WebMatrix.WebData.SimpleRoleProvider.get_PreviousProvider()
在WebMatrix.WebData.SimpleRoleProvider.RoleExists(字符串角色名称)
在 System.Web.Security.Roles.RoleExists(字符串角色名称)
在 GratifyGaming.Domain.Migrations.Configuration.Seed(GratifyGamingContext
上下文)在 C:\Users\Unreal\Documents\Visual Studio 中
2010\Projects\GratifyGaming\GratifyGaming.Domain\Migrations\Configuration.cs:行
36
在 System.Data.Entity.Migrations.DbMigrationsConfiguration1.OnSeed(DbContext
context)
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1
endingMigrations、字符串 targetMigrationId、字符串 lastMigrationId)
在 System.Data.Entity.Migrations.Infrastruct.MigratorLoggingDecorator.Upgrade(IEnumerable`1
endingMigrations、字符串 targetMigrationId、字符串 lastMigrationId)
在 System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration)
在 System.Data.Entity.Migrations.Infrastruct.MigratorBase.Update(字符串
目标迁移)
在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
在调用“WebSecurity”类的任何其他方法之前,必须先调用“WebSecurity.InitializeDatabaseConnection”方法。
此调用应放置在根目录下的 _AppStart.cshtml 文件中
你的网站。
有问题的代码行是Role.Exists
我尝试将 WebSecurity.InitializeDatabaseConnection 放入 Global.asax、Seed() 中,并创建了 _AppStart.cshtml 但没有成功。我在互联网上寻找可能的解决方案,但没有一个有效(包括其他堆栈溢出文章)。下面是一些值得注意的博客文章。
- http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/
- http://odetocode.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx http://odetocode.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx
请参阅下面的代码。
[配置.cs]
protected override void Seed(GratifyGaming.Domain.Models.DAL.GratifyGamingContext context)
{
var criteria = new List<Criterion>
{
new Criterion { ID = 1, IsMandatory=true, Name = "Gameplay", Description="The playability of the games core mechanics" },
new Criterion { ID = 2, IsMandatory=true, Name = "Art Style", Description="The artistic feel of the game as a whole. Elements such as story, style and originality come into play." },
new Criterion { ID = 3, IsMandatory=true, Name = "Longevity", Description="How long did this game keep you entertained?" },
new Criterion { ID = 4, IsMandatory=true, Name = "Graphics", Description="How good does the game look?" }
};
criteria.ForEach(s => context.Criterion.AddOrUpdate(s));
context.SaveChanges();
if (!Roles.RoleExists("Administrator"))
Roles.CreateRole("Administrator");
if (!WebSecurity.UserExists("user"))
WebSecurity.CreateUserAndAccount(
"user",
"password");
if (!Roles.GetRolesForUser("lelong37").Contains("Administrator"))
Roles.AddUsersToRoles(new[] { "user" }, new[] { "Administrator" });
}
Criterion 种子代码可以正常工作。
[_AppStart.cshtml]
@{
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId",
"UserName", autoCreateTables: true);
}
}
正常登录我的网站与此处的此功能完美配合。
[网络配置]
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear/>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
[AccountModel.cs]
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public virtual ICollection<Game> AttachedGames { get; set; }
public virtual ICollection<UserGratificationRecord> GratificationHistory { get; set; }
[ForeignKey("UserLevel")]
public int? AcheivementID { get; set; }
public virtual Acheivement UserLevel { get; set; }
public int? NumOfGratifictions { get; set; }
}
UPDATE
我认为 WebSecurity.InitializeDatabaseConnection 甚至没有运行 - 我可以在我的 Seed 方法中放置多个,并且不会出现通常会出现的“只能调用一次”错误。
我的种子方法与所有模型一起位于我的域项目中,而其他所有内容都位于 WebUI 项目中。不确定这是否与此有关。