问题描述
我正在尝试将用户和角色植入我的数据库.当前在C#MVC4中使用具有自动迁移功能的Code First Entity Framework.每当我打电话
I am trying to seed users and roles into my database.Currently using Code First Entity Framework with Automatic Migrations in C# MVC4.Whenever I call
我收到以下错误:
令人反感的代码行是Role.Exists
我尝试将WebSecurity.InitializeDatabaseConnection放在Global.asax,Seed()中,并创建了_AppStart.cshtml,但没有成功.我已经在互联网上搜寻了一个可能的解决方案,但没有一个起作用(包括其他有关堆栈溢出的文章).下面是一些著名的博客文章.
I have tried putting the WebSecurity.InitializeDatabaseConnection in Global.asax, Seed(), and created an _AppStart.cshtml with no success. I have trawled the internet looking for a possible solution and none of them have worked (including other stack overflow articles). Some notable blog posts are below.
- 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://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
请参见下面的代码.
[Configuration.cs]
[Configuration.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" });
}
条件种子代码可以正常工作.
The Criterion seed code works without fail.
[_ AppStart.cshtml]
[_AppStart.cshtml]
@{
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId",
"UserName", autoCreateTables: true);
}
}
正常登录到我的网站可以在此正常使用.
Normal login to my site works perfectly with this located here.
[web.config]
[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]
[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; }
}
更新
我认为WebSecurity.InitializeDatabaseConnection甚至都没有运行-我可以在我的Seed方法中放置多个,并且不会出现通常会出现的只能被调用一次"错误.
I think the WebSecurity.InitializeDatabaseConnection is not even being run - I can place more than one in my Seed method, and not get the 'can only be called once' error that you would normally get.
我的种子方法和所有模型都在我的域项目中,而其他所有内容都在WebUI项目中.不确定是否与此有关.
My seed method is in my domain project along with all the models, while everything else is in a WebUI project. Not sure if this has anything to do with it.
推荐答案
删除现有对WebMatrix.WebData的引用添加参考WebMatrix.WebData版本2.错误将停止.
Delete your existing reference to WebMatrix.WebDataAdd reference WebMatrix.WebData version 2.Error will stop.
这篇关于无法为用户& amp;的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!