C# ASP.NET-获取种子设定的用户ID

C# ASP.NET-获取种子设定的用户ID,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在做一个代码优先的数据库,我想知道如何获得我存储的种子用户的ID 在这部分代码中,我创建了用户 //Users List<ApplicationUser> applicationUsers = new List<ApplicationUser>(); applicationUsers.Add(new ApplicationUser() { Email = "person1@test.test", UserName =

我正在做一个代码优先的数据库,我想知道如何获得我存储的种子用户的ID

在这部分代码中,我创建了用户

         //Users
        List<ApplicationUser> applicationUsers = new List<ApplicationUser>();
        applicationUsers.Add(new ApplicationUser() { Email = "person1@test.test", UserName = "person1@test.test" });
        applicationUsers.Add(new ApplicationUser() { Email = "person2@test.test", UserName = "person2@test.test" });
        applicationUsers.Add(new ApplicationUser() { Email = "person3@test.test", UserName = "person3@test.test" });
        applicationUsers.Add(new ApplicationUser() { Email = "person4@test.test", UserName = "person4@test.test" });
        applicationUsers.Add(new ApplicationUser() { Email = "person5@test.test", UserName = "person5@test.test" });

        foreach (ApplicationUser user in applicationUsers)
        {
            manager2.Create(user, "qweQWE123!@#");
            manager2.AddToRole(user.Id, "RegisteredUser");
        }
这是我填写项目的方式

itemList.Add(new ItemModels() { ItemTypeID = 1, ItemPrice = 1.67,  ItemQuantity = 1, QualityID = 1, UserId="What do i put here?" });

在循环中,您已经在访问
manager2.AddToRole()
中的user.Id属性

项目播种的最简单方法是在该循环中创建项目对象,然后在添加所有项目后保存

var itemList = new List<ItemModels>();

foreach (ApplicationUser user in applicationUsers)
{
   manager2.Create(user, "qweQWE123!@#");
   manager2.AddToRole(user.Id, "RegisteredUser");

   // use user.Id for ItemModel then add to list
   itemList.Add(new ItemModels() { UserId = user.Id, ItemTypeID = 1, ItemPrice = 1.67,  ItemQuantity = 1, QualityID = 1 });

}

// then save your items here
context.Items.AddToRange(itemList);
context.SaveChanges();
var itemList=新列表();
foreach(applicationUsers中的ApplicationUser用户)
{
manager2.创建(用户“qweQWE123@#”);
manager2.AddToRole(user.Id,“RegisteredUser”);
//将user.Id用于ItemModel,然后添加到列表中
添加(新的ItemModels(){UserId=user.Id,ItemTypeID=1,ItemPrice=1.67,ItemQuantity=1,QualityID=1});
}
//然后在此处保存您的项目
context.Items.AddToRange(itemList);
SaveChanges();

用户管理器。是否创建
返回创建的用户?您应该能够从以下位置获取信息:there@pinkfloydx33UserManager.Create返回IdentityResult,如果创建成功与否,它只包含一个布尔值。如果您是对的,Create会在传递给它的用户中填充Id属性(您在下一行使用它)。。。那你为什么不能用它呢?
var itemList = new List<ItemModels>();

foreach (ApplicationUser user in applicationUsers)
{
   manager2.Create(user, "qweQWE123!@#");
   manager2.AddToRole(user.Id, "RegisteredUser");

   // use user.Id for ItemModel then add to list
   itemList.Add(new ItemModels() { UserId = user.Id, ItemTypeID = 1, ItemPrice = 1.67,  ItemQuantity = 1, QualityID = 1 });

}

// then save your items here
context.Items.AddToRange(itemList);
context.SaveChanges();