Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one put application users in the same context as the rest of the objects?

The stock asp.net mvc 5 application creates the application users, aka identity users in a separate context, named a file called "IdentityModels.cs" - it looks like this

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
}

I am attempting to put the Application users in a regular data context, i.e. something like this

 public class BlogProphetContext : DbContext
    {

        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<Answer> Answers { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<Tag> Tags { get; set; }
    }

However, every time I do that I get the following error every time I try to create an account

The UserId field is required

In the AccountController.cs when I attempt to execure the following line of code

result = await UserManager.AddLoginAsync(user.Id, info.Login);

I get the feeling my approach is wrong, and that I cannot have the ApplicationUsers in the main data context file without some sort of external chicanery - does anyone know of some way to do this? All of the files are up to date.

like image 787
Steve French Avatar asked Oct 28 '13 06:10

Steve French


1 Answers

This was a bit too easy - it turns out that all you have to do it remove the

<ApplicationUser> 

when you call the context and everything turns out like you might expect (i.e. the MVC assumptions and the developers assumptions (in this case, mine) sync.

Here is is working properly

 public class MyContext : IdentityDbContext
    {
        public MyContext()
            : base("DefaultConnection")
        {
        }
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<Answer> Answers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        }
    }
like image 190
Steve French Avatar answered Sep 22 '22 04:09

Steve French