Asp.net mvc 无法为用户添加种子&;角色
我正在尝试将用户和角色植入我的数据库。 目前正在使用代码优先实体框架,并在C#MVC4中进行自动迁移。 每当我打电话 更新数据库-部队 我得到以下错误: 运行种子法。 System.InvalidOperationException:必须调用 在调用任何 “WebSecurity”类的其他方法。此呼叫应置于 站点根目录中的_AppStart.cshtml文件。 在WebMatrix.WebData.SimpleRoleProvider.get_PreviousProvider()中 在WebMatrix.WebData.SimpleRoleProvider.RoleExists(字符串roleName)中 位于System.Web.Security.Roles.RoleExists(字符串roleName) 在sappyigaming.Domain.Migrations.Configuration.Seed(sappyigamingcontext 上下文)在C:\Users\Unreal\Documents\Visual Studio中 2010\Projects\expectivegaming\expectivegaming.Domain\Migrations\Configuration.cs:line 36 在System.Data.Entity.Migrations.DbMigrationsConfigurationAsp.net mvc 无法为用户添加种子&;角色,asp.net-mvc,entity-framework,c#-4.0,seed,simplemembership,Asp.net Mvc,Entity Framework,C# 4.0,Seed,Simplemembership,我正在尝试将用户和角色植入我的数据库。 目前正在使用代码优先实体框架,并在C#MVC4中进行自动迁移。 每当我打电话 更新数据库-部队 我得到以下错误: 运行种子法。 System.InvalidOperationException:必须调用 在调用任何 “WebSecurity”类的其他方法。此呼叫应置于 站点根目录中的_AppStart.cshtml文件。 在WebMatrix.WebData.SimpleRoleProvider.get_PreviousProvider()中 在WebMa
1.OnSeed(DbContext
(上下文)
位于System.Data.Entity.Migrations.DbMigrator.SeedDatabase()处
位于System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()处
在System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1
pendingMigrations,字符串targetMigrationId,字符串lastMigrationId)
位于System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1
pendingMigrations,字符串targetMigrationId,字符串lastMigrationId)
位于System.Data.Entity.Migrations.DbMigrator.Update(字符串targetMigration)
位于System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串
目标(迁移)
在System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()中
位于System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()处
在调用“WebSecurity”类的任何其他方法之前,必须先调用“WebSecurity.InitializeDatabaseConnection”方法。
此调用应放在的根目录中的_AppStart.cshtml文件中
你的网站
有问题的代码行是角色。存在
我尝试将WebSecurity.InitializeDatabaseConnection放入Global.asax,Seed(),并创建了一个_AppStart.cshtml,但没有成功。我在互联网上搜寻了一个可能的解决方案,但没有一个有效(包括其他堆栈溢出文章)。下面是一些值得注意的博客文章
请参阅下面的代码
[配置.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" });
}
正常登录到我的网站与此完美地位于这里
[web.config]
<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; }
}
[表(“用户配置文件”)]
公共类用户配置文件
{
[关键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共虚拟ICollection AttachedGames{get;set;}
公共虚拟ICollection满足历史记录{get;set;}
[外键(“用户级别”)]
public int?AcheivementID{get;set;}
公共虚拟访问用户级别{get;set;}
public int?numf满足{get;set;}
}
更新
我认为WebSecurity.InitializeDatabaseConnection甚至没有运行——我可以在我的Seed方法中放置多个,而不会出现通常会出现的“只能调用一次”错误
我的种子方法和所有模型都在我的域项目中,而其他的都在WebUI项目中。不确定这是否与此有关。只需将惰性init放入种子方法的顶部即可
protected override void Seed(GratifyGaming.Domain.Models.DAL.GratifyGamingContext context)
{
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection",
"UserProfile",
"UserId",
"UserName",
autoCreateTables: true);
}
在应用程序_Start中,尝试添加:
var configuration = new Data.Migrations.Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();
您必须将configuration.cs文件公开
public class Configuration : DbMigrationsConfigurati
这将使您的seed方法在运行程序时被调用删除对WebMatrix.WebData的现有引用
添加参考WebMatrix.WebData版本2。
错误将停止。谢谢您的输入TMan。添加您建议的代码会出现以下错误:错误6命名空间“WebMatrix.Data”中不存在类型或命名空间名称“Migrations”(是否缺少程序集引用?)有趣的是,它找不到WebSecurity.Initialized(在种子方法中)-错误3“WebMatrix.WebData.WebSecurity”不包含“Initialized”的定义。我在域项目中引用了WebMatrix.WebData的旧版本。我引用了v2,添加了代码,现在它可以工作了。+1用于if(!WebSecurity.Initialized)。我的Seed()方法被调用了两次!奇怪的是,第二个调用似乎是从Seed方法本身内部触发的(即,从一行代码开始,在该行中我访问迁移期间新创建的一个dbset)。如果没有WebSecurity.Initialized check,则第二个Seed()调用会导致“WebSecurity.InitializeDatabaseConnection”方法只能调用一次。请如何引用WebMatrix.WebData版本2?您可以将其添加到web.config
public class Configuration : DbMigrationsConfigurati