Solution - The entity type ApplicationUser is not part of the model for the current context

断了今生、忘了曾经 提交于 2020-01-11 07:09:08

问题


I am posting this question as documentation because it took me many hours to find this simple issue. I am taking the original MVC project generated by VS15 and attempting to modify it.

Error: [InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.]


回答1:


You may get this error for a variety of reasons. This is not a guaranteed fix but hopefully it will save some time for someone out there. This error shows up usually because your application is using a default DbContext and not the one you're intending to use.

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

The project generates this custom DbContext. You should make sure the "DefaultConnection" is either the connection string or the name of the connection in your Web.config of your project using this class.

This is my connection string. Note that the name is the same as the one in the DbContext above:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-AspnetIdentitySample-10_8_3;Integrated Security=True" providerName="System.Data.SqlClient" />

Once you made sure your connection string is set up, we need to check the bindings in NinjectWebCommon.

kernel.Bind<ApplicationDbContext>().ToSelf();
kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>().WithConstructorArgument("context", kernel.Get<ApplicationDbContext>());
kernel.Bind<UserManager<ApplicationUser>>().ToSelf();

This is where I found the error in my application. The first line is not required but is a good idea. The second line is the most important.

UserStore has 2 constructors. The first is empty. The second has 1 parameter, DbContext context. Ninject's constructor arguments are case-sensitive and must be named "context" for Ninject to properly select this constructor.



来源:https://stackoverflow.com/questions/33578999/solution-the-entity-type-applicationuser-is-not-part-of-the-model-for-the-curr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!