如何在迁移配置文件中获取和使用 AspnetUsers UserId?
How can i get and use AspnetUsers UserId in migration configuration file?
这是entity framework迁移的配置文件(种子方法)的代码。
通过这段代码,我为 AspnetUsers table 创建了一个用户和角色,并将该用户置于名为 Admin 的角色中。
AppUserManager userMgr = new AppUserManager(new UserStore<AppUser>(context));
AppRoleManager roleMgr = new AppRoleManager(new RoleStore<AppRole>(context));
string roleName = "Admin";
string userName = "admin@educationboard.com";
string password = "Sifre";
string email = "admin@educationboard.com";
if (!roleMgr.RoleExists(roleName))
{
roleMgr.Create(new AppRole(roleName));
}
AppUser user = userMgr.FindByName(userName);
if (user == null)
{
userMgr.Create(new AppUser { UserName = userName, Email = email },
password);
user = userMgr.FindByName(userName);
}
if (!userMgr.IsInRole(user.Id, roleName))
{
userMgr.AddToRole(user.Id, roleName);
}
foreach (AppUser dbUser in userMgr.Users)
{
dbUser.Cinsiyet = eCinsiyetler.Erkek;
}
context.SaveChanges();
然后我还创建了一个名为 Articles 的实体,每篇文章都有一个作者 ID。我在 Article 实体中将 AuthorId 命名为 UserId。我怎样才能在下面的代码中获取和使用我刚刚在上面创建的 UserId?
var articles= new List<Article>
{
new Article{Title="Title 1", AddedDate=DateTime.Now, Content="content here.", UserId=.(What code should be here? };
articles.ForEach(p => context.Articles.AddOrUpdate(s => s.Title, p));
context.SaveChanges();
为什么不能像上面那样查询用户?
AppUser user = userMgr.FindByName(userName);
if (user == null)
{
userMgr.Create(new AppUser { UserName = userName, Email = email },
password);
user = userMgr.FindByName(userName);
}
然后有user.Id?
如果您在 ApplicationDbContext 中有句柄,您可以轻松地执行类似
的操作
ApplicationDbContext db = new ApplicationDbContext();
var user = db.AspNetUser.Single(x => x.UserName == username);
var id = user.Id;
这是entity framework迁移的配置文件(种子方法)的代码。 通过这段代码,我为 AspnetUsers table 创建了一个用户和角色,并将该用户置于名为 Admin 的角色中。
AppUserManager userMgr = new AppUserManager(new UserStore<AppUser>(context));
AppRoleManager roleMgr = new AppRoleManager(new RoleStore<AppRole>(context));
string roleName = "Admin";
string userName = "admin@educationboard.com";
string password = "Sifre";
string email = "admin@educationboard.com";
if (!roleMgr.RoleExists(roleName))
{
roleMgr.Create(new AppRole(roleName));
}
AppUser user = userMgr.FindByName(userName);
if (user == null)
{
userMgr.Create(new AppUser { UserName = userName, Email = email },
password);
user = userMgr.FindByName(userName);
}
if (!userMgr.IsInRole(user.Id, roleName))
{
userMgr.AddToRole(user.Id, roleName);
}
foreach (AppUser dbUser in userMgr.Users)
{
dbUser.Cinsiyet = eCinsiyetler.Erkek;
}
context.SaveChanges();
然后我还创建了一个名为 Articles 的实体,每篇文章都有一个作者 ID。我在 Article 实体中将 AuthorId 命名为 UserId。我怎样才能在下面的代码中获取和使用我刚刚在上面创建的 UserId?
var articles= new List<Article>
{
new Article{Title="Title 1", AddedDate=DateTime.Now, Content="content here.", UserId=.(What code should be here? };
articles.ForEach(p => context.Articles.AddOrUpdate(s => s.Title, p));
context.SaveChanges();
为什么不能像上面那样查询用户?
AppUser user = userMgr.FindByName(userName);
if (user == null)
{
userMgr.Create(new AppUser { UserName = userName, Email = email },
password);
user = userMgr.FindByName(userName);
}
然后有user.Id?
如果您在 ApplicationDbContext 中有句柄,您可以轻松地执行类似
的操作ApplicationDbContext db = new ApplicationDbContext();
var user = db.AspNetUser.Single(x => x.UserName == username);
var id = user.Id;