我已将AspNetUser(标识)tabe更改为仅包含以下代码的用户。
<!DOCTYPE html>
<html>
<head>
<script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/react-dom.min.js"></script>
<script src=bower_components/babel/browser.min.js"></script>
</head>
<body>
<div id="my-div"></div>
<script type="text/babel">
var Message = React.createClass({
render:function(){
return <h1>Hello, world!</h1>
}
});
ReactDOM.render(<Message/>, document.getElementById('my-div'));
</script>
</body>
</html>
我的申请工作正常,但不是我的种子。我在Migrations / Configuration.cs中有这段代码
// Change the name of the table to be Users instead of AspNetUsers
modelBuilder.Entity<IdentityUser>()
.ToTable("Users").Property(p => p.Id).HasColumnName("UserID");
modelBuilder.Entity<ApplicationUser>()
.ToTable("Users").Property(p => p.Id).HasColumnName("UserID");
我收到错误消息&#34;找不到UserId&#34;。好像我的UserManager.Create失败了。
如何更改种子代码以使用标准ID的用户ID?
答案 0 :(得分:0)
实际上,当您保存用户时,它还没有给出ID,您必须在创建用户和用户之间检索用户。 addToRole:
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
string role = "Admin";
string password = "Password@1234";
//Create Role Admin if it does not exist
if (!RoleManager.RoleExists(role))
{
var roleresult = RoleManager.Create(new IdentityRole(role));
}
//Create Admin users with password=123456
var admin1 = new ApplicationUser();
admin1.UserName = "admin1@admin1.com";
admin1.Email = "admin1@admin1.com";
admin1.EmailConfirmed = true;
UserManager.Create(admin1, password);
// Refetch user with ID:
dbAdmin1 = context.Users.FirstOrDefault(x => x.UserName == admin1.UserName);
UserManager.AddToRole(dbAdmin1.Id, role);
context.SaveChanges();