让我让你明白,在我决定发布这个问题之前,我已经搜索了很多stackoverflow线程并实现了几乎所有规定的解决方案都无济于事。我试图了解如何将身份系统中的用户与项目中的其他实体相关联,因此我按照我发现的here教程进行了操作。 当我到脚手架创建一个使用ToDo.cs模型和ToDoContext.cs的ToDo控制器的时候,我总是遇到障碍,无论我做了什么,我都无法用脚手架。以下是我目前的
public class ToDo
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsDone { get; set; }
public virtual MyUser User { get; set; }
}
public class MyUser : IdentityUser
{
public string HomeTown { get; set; }
public ICollection<ToDo> ToDoes { get; set; }
}
public class ToDoContext : IdentityDbContext<MyUser>
{
public ToDoContext()
:base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
base.OnModelCreating(modelBuilder);
}
public DbSet<ToDo> ToDoes { get; set; }
}
我在Global.asax中
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer<ToDoContext>(new ToDoInitializer());
}
}
public class ToDoInitializer : DropCreateDatabaseAlways<ToDoContext>
{
protected override void Seed(ToDoContext context)
{
var UserManager = new UserManager<MyUser>
(
new UserStore<MyUser>(context)
);
var RoleManager = new RoleManager<IdentityRole>
(
new RoleStore<IdentityRole>(context)
);
string name = "Admin";
string password = "123456";
//Create Role Admin if it does not exist
if (!RoleManager.RoleExists(name))
{
var roleresult = RoleManager.Create(new IdentityRole(name));
}
//Create User=Admin with password=123456
var user = new MyUser();
user.UserName = name;
var adminresult = UserManager.Create(user, password);
//Add User Admin to Role Admin
if (adminresult.Succeeded)
{
var result = UserManager.AddToRole(user.Id, name);
}
base.Seed(context);
}
}
每当我试图脚手架时,我都会附上这个错误 1: