The entity type ApplicationUser is not part of the model for the current context
Solution 1
for me it seems to miss a context instanciation:
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
should be
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
Solution 2
I was having this same problem. I’m doing database first development with an EDMX file.
If you are using the connection string generated when adding the EDMX file in :base(“EDMXConnString”)
you will most likely have this problem.
I fixed this by creating a standard connection string that pointed to the database where the ASP.NET Identity tables are.
<add name="MyConnString" connectionString="Data Source=server; Initial Catalog=db_name; User ID=user_id; Password=password; Connect Timeout=60;" providerName="System.Data.SqlClient" />
And then used that connection string in :base
, and it worked!
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnString")
{
}
}
Solution 3
My problem was I tried to use generated ADO.NET connection string for both generated and authentication context ApplicationDbContext
. I fixed it by using a separate connection string for authentication. Also pay attention to the provider - for authentication context it has to be System.Data.SqlClient
:
<add name="DefaultConnection" connectionString="Server=qadb.myserver.com;Database=mydb;User Id=myuser;Password=mypass;" providerName="System.Data.SqlClient" />
Solution 4
If you are using code first, check your connection string to ensure providerName is 'SqlClient' as in providerName="System.Data.SqlClient
If you are using database first, check your connection string to ensure providerName is 'EntityClient' as in providerName="System.Data.EntityClient
Solution 5
Same problem to me, it solved by this code:
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{
Database.Connection.ConnectionString = @"data source=...;initial catalog=...;user id=...;password=...;multipleactiveresultsets=True;application name=EntityFramework";
}
Comments
-
Marc over 1 year
I'm migrating from Identity 1.0.0 to Identity 2.0.1 following this article
and the migrations code generated is nothing about the new IdentityUser. It doesn't add the new columns.
So I made a new project and tried again but the migrations codes is empty.
To fix that problem, I did the edits directly in SQL Server and imported my database again in my solution.
Now my
AspNetUser
is exactly the same as myIdentityUser
as you can seeIdentityUser
public virtual int AccessFailedCount { get; set; } public virtual ICollection<TClaim> Claims { get; } public virtual string Email { get; set; } public virtual bool EmailConfirmed { get; set; } public virtual TKey Id { get; set; } public virtual bool LockoutEnabled { get; set; } public virtual DateTime? LockoutEndDateUtc { get; set; } public virtual ICollection<TLogin> Logins { get; } public virtual string PasswordHash { get; set; } public virtual string PhoneNumber { get; set; } public virtual bool PhoneNumberConfirmed { get; set; } public virtual ICollection<TRole> Roles { get; } public virtual string SecurityStamp { get; set; } public virtual bool TwoFactorEnabled { get; set; } public virtual string UserName { get; set; }
IdentityUser.cs
public class ApplicationUser : IdentityUser { public bool Has_accepted_policy { get; set; } public int user_type_id { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } }
AspNetUser
public string Id { get; set; } [Required] [StringLength(256)] public string UserName { get; set; } public string PasswordHash { get; set; } public string SecurityStamp { get; set; } [StringLength(256)] public string Email { get; set; } public bool EmailConfirmed { get; set; } public bool Is_Active { get; set; } [Required] [StringLength(128)] public string Discriminator { get; set; } public int? user_type_id { get; set; } public bool Has_accepted_policy { get; set; } public string PhoneNumber { get; set; } public bool PhoneNumberConfirmed { get; set; } public bool TwoFactorEnabled { get; set; } public DateTime? LockoutEndDateUtc { get; set; } public bool LockoutEnabled { get; set; } public int AccessFailedCount { get; set; } ... other virtual properties
and when I try to register a user I have the following exception
The entity type ApplicationUser is not part of the model for the current context
at this line
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
My startup.Auth.cs
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
And in my AccountController I declare my
UserManager
like thispublic AccountController() : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat) { } public AccountController(UserManager<ApplicationUser> userManager, ISecureDataFormat<AuthenticationTicket> accessTokenFormat) { UserManager = userManager; AccessTokenFormat = accessTokenFormat; } public UserManager<ApplicationUser> UserManager { get; private set; }
I haven't changed anything except the new properties in the
AspNetUser
class and it used to work well before the migration.There's a similar issue on CodePlex marked as fixed but they don't give the solution
Does anyone know how to fix this?
EDIT
To be sure I didn't do any mistakes when I edited my SQL database. I created another project and generated an Identity database and I changed the connection string for that database and I still have the same error.
SOLUTION
When I have edited my database I haven't noticed that in Identity 2.0.0 they changed the
User_Id
forUserId
inAspUserClaims
table. After doing that I had the same error but then I did what tschmit007 said about adding theApplicationDbContext
to theUserStore
constructor and now it works.UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
-
Marc almost 10 yearsAt first I tried with
new MyDbContext
as a parameter for the UserStore constructor, im gonna try withApplicationUserDbContext
-
tschmit007 almost 10 years@Marc sorry, it is
ApplicationDbContext
in your case -
Marc almost 10 yearsThe model backing the 'ApplicationDbContext' context has changed since the database was created. I will try to do a migration to see what has changed
-
Marc almost 10 yearsThe migrations code drops everything and creates my database from scratch. Is it normal? Why do I have 2 contexts. ApplicationDbContext and my myDbContext. I'm confused
-
Marc almost 10 yearsAlso I noticed that the migrations code creates the old version of my AspNetUser
-
Marc almost 10 yearsDo you have an idea why?
-
Marc almost 10 yearsIt works... if I follow your answer with the new database. the only difference with my old one is that they changed User_Id for UserId in the AspUserClaims. Thats why your answer wasn't working at first I guess.
-
SeraM about 9 yearsThis also fixed the problem for me (in a database first development situation).
-
Transformer about 7 yearshi, after much searching I found this I have a similar problem, in which 2 files/classes did you find the section applicationUser, and which one did you delete
-
Ortund over 6 yearsI completely forgot to add thee context in on the UserStore as well /facepalm
-
solujic about 6 yearsConnection String correction works, but if anyone has problem with "invalid column name Hometown" error just delete hometown field from IdentityModels.cs and it's usage in AccountController, I used older script for genereting tables on db and I guees they don't have hometown field therefore I was gettin this error...
-
Jansen over 4 yearsIt's already 2019 and 2020 is coming. It is still working. Nice.
-
Brendan Sluke over 4 yearsWorks like a charm, I do hate having to create an additional connection string. But nonetheless it works!
-
Fahad S. Ali over 3 years@James Does this mean that we need two connection string in the web-config file? One for the Identity Model and One for the EDMX model?
-
Leonel Gonzalez over 2 yearsThat worked for me - it created the OWIN schema and everything - thanks!