How can one put application users in the same cont

2019-01-22 14:41发布

问题:

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.

回答1:

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>();
        }
    }


回答2:

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; }
}


回答3:

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



回答4:

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.