我使用一个称为Seeds的静态类,该类使用Program.cs中的数据样本为数据库播种:
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
// Get services
var services = scope.ServiceProvider;
// Create/Seed the database
Seeds.SeedDatabase(services);
// Create the roles for the application
var serviceProvider = services.GetRequiredService<IServiceProvider>();
var configuration = services.GetRequiredService<IConfiguration>();
Seeds.CreateRoles(serviceProvider, configuration).Wait();
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
}
这是种子:
public class Seeds
{
public static void SeedDatabase(IServiceProvider serviceProvider)
{
// Seed DB
using (var _db = new AppDbContext(serviceProvider.GetRequiredService<DbContextOptions<AppDbContext>>()))
{
Console.WriteLine("\n Looking for a database... \n");
// Look for a database
if (!_db.Database.EnsureCreated())
{
// Debug message
string message = "\n There is already a database. \n";
Console.WriteLine(message);
// DB has been seeded before
}
else
{
// Debug message
string message = "\n A new database has been created. \n";
Console.WriteLine(message);
_db.FlightSettings.Add(
new FlightSetting(
...Variables...
),
);
...Here other values are seeded as FlightSettings...
// Save the data samples
_db.SaveChanges();
// DB has been seeded now
}
}
}
}
public static async Task CreateRoles(IServiceProvider serviceProvider)
{
//adding customs roles
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<AppUser>>();
// Roles in the project
string[] roleNames = { "Pilot", "Office" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
// creating the roles and seeding them to the database
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
// create custom users
var officeUser = new AppUser
{
UserName = "paulita",
Email = "paulita@",
Password = "paulita"
};
var pilots = new AppUser[] {
new AppUser
{
Name = "pablito",
IdentityDocument = 80421514,
EmployeeNumber = 52641958,
UserName = "pcastellanos",
Email = "pablito@",
Password = "pablito",
BornDate = new DateTime(1990, 6, 20)
},
new AppUser
{
Name = "pedrito",
IdentityDocument = 1098808192,
EmployeeNumber = 62549214,
UserName = "privero",
Email = "pedrito@",
Password = "pedrito",
BornDate = new DateTime(1992, 8, 10)
}
};
foreach (var pilot in pilots)
{
await UserManager.CreateAsync(pilot, pilot.Password);
await UserManager.AddToRoleAsync(pilot, "Pilot");
}
await UserManager.CreateAsync(officeUser, officeUser.Password);
await UserManager.AddToRoleAsync(officeUser, "Office");
using (var _db = new AppDbContext(serviceProvider.GetRequiredService<DbContextOptions<AppDbContext>>()))
{
var myPilots = await UserManager.GetUsersInRoleAsync("Pilot");
AppUser myUser = await UserManager.FindByNameAsync("pcastellanos");
var flightSettings = await _db.FlightSettings.ToListAsync();
await _db.PilotTests.AddAsync(
new PilotTest
{
Pilot = myUser,
SimulationSetting = flightSettings[0]
}
);
// Save the data samples
_db.SaveChanges();
您将在此行中看到
var myPilots = await UserManager.GetUsersInRoleAsync("Pilot");
我向数据库请求“飞行员”角色的所有用户。我在创建PilotTest时遇到错误,因为在PilotTest中,它不仅尝试引用相应的AppUser,而且还会尝试再次创建AppUser,并且由于用户已经存在,VisualStudio告诉我数据库中已有一个条目,那个Primay Key。
每个PiloTest应该引用一个飞行员。这是数据库中的PilotTest表:
我试图在此处添加一个飞行员:
AppUser myUser = await UserManager.FindByNameAsync("pcastellanos");
但同样如此。
这是两个类:
public class PilotTest
{
// Unique ID
[Key]
public string Id { get; set; }
// The pilot responsible for this test
public AppUser Pilot { get; set; }
// FlightSetting of the simulation
public FlightSetting SimulationSetting { get; set; }
...Other variables...
}
public class AppUser : IdentityUser
{
// Custom variables on users
public string Name { get; set; }
public int IdentityDocument { get; set; }
public int EmployeeNumber { get; set; }
public DateTime BornDate { get; set; }
// For debugging purposes
public string Password { get; set; }
}
我的理论是我做错了事,但是由于几天前我找不到任何解决方法。
答案 0 :(得分:0)
对于您当前的定义密码paulita
,根据默认密码规则,该密码无效。您可以像下面那样更改代码,检查result
会发现错误。
foreach (var pilot in pilots)
{
var result = await UserManager.CreateAsync(pilot, pilot.Password);
await UserManager.AddToRoleAsync(pilot, "Pilot");
}
如果您希望使用此密码,则需要像这样配置IdentityOptions
services.AddDefaultIdentity<AppUser>(options => {
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireDigit = false;
})
这是我当前的Seeds
public class Seeds
{
public static void SeedDatabase(IServiceProvider serviceProvider)
{
// Seed DB
using (var _db = serviceProvider.GetRequiredService<TestMVC2_1Context>())
{
Console.WriteLine("\n Looking for a database... \n");
// Look for a database
if (!_db.Database.EnsureCreated())
{
// Debug message
string message = "\n There is already a database. \n";
Console.WriteLine(message);
// DB has been seeded before
}
else
{
// Debug message
string message = "\n A new database has been created. \n";
Console.WriteLine(message);
// Save the data samples
_db.SaveChanges();
// DB has been seeded now
}
}
}
public static async Task CreateRoles(IServiceProvider serviceProvider)
{
//adding customs roles
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<AppUser>>();
// Roles in the project
string[] roleNames = { "Pilot", "Office" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
// creating the roles and seeding them to the database
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
// create custom users
var officeUser = new AppUser
{
UserName = "paulita",
Email = "paulita@",
Password = "paulita"
};
var pilots = new AppUser[] {
new AppUser
{
Name = "pablito",
IdentityDocument = 80421514,
EmployeeNumber = 52641958,
UserName = "pcastellanos",
Email = "pablito@",
Password = "pablito",
BornDate = new DateTime(1990, 6, 20)
},
new AppUser
{
Name = "pedrito",
IdentityDocument = 1098808192,
EmployeeNumber = 62549214,
UserName = "privero",
Email = "pedrito@",
Password = "pedrito",
BornDate = new DateTime(1992, 8, 10)
}
};
foreach (var pilot in pilots)
{
var result = await UserManager.CreateAsync(pilot, pilot.Password);
await UserManager.AddToRoleAsync(pilot, "Pilot");
}
await UserManager.CreateAsync(officeUser, officeUser.Password);
await UserManager.AddToRoleAsync(officeUser, "Office");
using (var _db = serviceProvider.GetRequiredService<TestMVC2_1Context>())
{
var myPilots = await UserManager.GetUsersInRoleAsync("Pilot");
AppUser myUser = await UserManager.FindByNameAsync("pcastellanos");
await _db.PilotTests.AddAsync(
new PilotTest
{
Pilot = myUser,
}
);
// Save the data samples
_db.SaveChanges();
}
}
}