0

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 enter image description here 1:

http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx

ibnhamza
  • 861
  • 1
  • 15
  • 29
  • Try removing the first three lines of your OnModelCreating (the ones with HasKey). Those aren't necessary because they are mapped in base.OnModelCreating (a method from the base class IdentityDbContext). – Augusto Barreto Mar 03 '15 at 15:47
  • @AugustoBarreto i did that but the problem still persist. – ibnhamza Mar 03 '15 at 16:01
  • :/ your code seems fine for me. Maybe this is somehow related to the scaffolding mechanism. Here is a possible cause: http://stackoverflow.com/a/25344594/1454888 . Also, just in case try rebuilding your project: http://stackoverflow.com/a/5697221/1454888 – Augusto Barreto Mar 03 '15 at 16:29
  • Did that lot of times, still the same. Getting fed up already. – ibnhamza Mar 03 '15 at 16:55
  • It somehow finally worked after i rebuilt the solution. – ibnhamza Mar 04 '15 at 06:16

0 Answers0