12

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.

4 Answers 4

11

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>();
        }
    }
1
  • Lifesaver, this was troubling me also. I hated the idea of having two contexts: one for the Identity subsystem and another for all the other data for my project!
    – xingyu
    Feb 14, 2014 at 3:29
3

In this case, if you dont want your ApplicationUser to be associated with asp.net Identity the easiest thing would be to just remove the inheritance from the applicationUser class.

Go from ApplicationUser : IdentityUser to just ApplicationUser, then in that class just create an Id property.

You wont have to use IdentityDBContext then.

Of course you will have to totally rewrite AccountController, or get rid of it. You cant use UserManager anyways as thats linked to IdentityUser

Then obviously make sure you add your migration/update database if you are working with EF.

If you do want to keep the account controller as is and keep IdentityDbContext but add other entities then thats easy, just do this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection")
    {
    }

    public DbSet<Dept> Dept{ get; set; }
    public DbSet<Course> Course{ get; set; }
    public DbSet<Etc> Etc{ get; set; }
}
2

Ensure that your ApplicationUser class inherits from IdentityUser.

Then you need to configure your context so that it maps all the ASP.NET Identity classes correctly. These are:

  1. ApplicationUser (your class)
  2. IdentityUserClaim
  3. IdentityUserLogin
  4. IdentityUserRole
  5. IdentityRole

Details of this is a bit too complicated to post here, but I have made a sample project that works with the release version of ASP.NET Identity. You can see it here: https://github.com/onybo/Asp.Net-Identity-sample-app/tree/master/CustomUser

3
  • I am actually not using the IdentityDbContext<ApplicationUser> at all, I would like to treat the ApplicationUser just like a any other class for (extensions and related properties, etc - it was working in the beta strangely) I wonder if there is any way of NOT using the IdentityDBContext at all. Oct 28, 2013 at 14:16
  • And I do have everything up to date with the latest releases - I'm updating the question now. Oct 28, 2013 at 17:25
  • Ok, I have updated my answer and included a link to a working project that I think does what you are looking for
    – Olav Nybø
    Oct 29, 2013 at 12:14
0

I am not sure if this will help you or if i fully understand your questions...but if you want try use your own "model" then change:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("PUT IN YOUR MODEL CONN HERE")
    {
    }

I do not know how much more than this you can configure or if this will really change the context.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.