entity framework - Creating Asp.net Identity user in Seed method of Db Initializer -
i have created data layer ef 6 code first , populating db through seed
method of evinitializer
class inheriting dropcreatedatabaseifmodelchanges
. implementation of seed
method
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. await cannot used in non- async method , cannot make seed async }
my question how can add user in seed
method using usermanager
class. when change var result = awit manager.createasync(user, "temp_123");
to
var result = manager.createasync(user, "temp_123").result; //or .wait
application hangs indefinitely
in asp.net-identity-2 usermanager has non async methods create.
var user = new applicationuser { email = "admin@myemail.com", username = "admin@myemail.com" }; manager.create(user, "temp_123");
same rolemanager if want create "admin" role.
var rolemanager = new rolemanager<identityrole>(new rolestore<identityrole>(context)); rolemanager.create(new role("admin"));
make user admin
manager.addtorole(user.id, "admin");
edit: trailmax commented, create()
extension method comes in microsoft.aspnet.identity
namespace not forget using microsoft.aspnet.identity
Comments
Post a Comment