我已经用EF 6代码创建了我的数据层,我通过从DropCreate
DatabaseIfModelChanges继承的EvInitializer类的种子方法填充数据库.种子方法的实现是
protected override void Seed(EvContext context) { //Add other entities using context methods ApplicationUserManager manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context)); var user = new ApplicationUser { Email = "admin@myemail.com",UserName = "admin@myemail.com"}; var result = await manager.CreateAsync(user,"Temp_123");//this line gives error. obvIoUsly await cannot be used in non- async method and I cannot make Seed async }
我的问题是如何使用UserManager类在Seed方法中添加一个用户.当我改变
var result = awit manager.CreateAsync(user,“Temp_123”);
至
var result = manager.CreateAsync(user,“Temp_123”). //或.Wait
应用程序无限期挂起
解决方法
在asp.net-identity-2中,usermanager具有非异步方法来创建.
var user = new ApplicationUser { Email = "admin@myemail.com",UserName = "admin@myemail.com" }; manager.Create(user,"Temp_123");
如果您要创建“admin”角色,则与rolemanager相同.
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); roleManager.Create(new Role("admin"));
使用户管理
manager.AddToRole(user.Id,"admin");
编辑:作为trailmax评论,Create()扩展方法与Microsoft.AspNet.Identity命名空间,所以不要忘记使用Microsoft.AspNet.Identity