Let me make you understand that before i decided to post this question, i have searched through many stackoverflow threads and implemented virtually all the solutions prescribed all to no avail. I was trying to understand how to associate users in the identity system with other entities in the project so i followed the tutorial i found here . When i got to the point of scaffolding to create a ToDo Controller using the ToDo.cs model and the ToDoContext.cs i kept hitting a snag and no matter what i did i just couldnt scaffold. Below is what i have currently
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; }
}
in Global.asax i have
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);
}
}
Whenever i tried to scaffold i get this error attached
1: