实体类型ApplicationUser不是当前上下文解决方案的模型的一部分

时间:2016-12-08 14:54:43

标签: entity-framework asp.net-web-api oauth-2.0 owin

因此,对于学校,我完成了创建API的任务,以便您可以登录数据库,登录后,您可以访问一些数据。要登录,我们将使用由另一个使用MVC的项目制作的AspNetUsers。我目前的代码:

AccountController.cs

namespace Klantenzone.API2.Controllers
{
    [RoutePrefix("api/Account")]
    public class AccountController : ApiController
    {
        private AuthRepository _repo = null;

        public AccountController()
        {
            _repo = new AuthRepository();
        }
    }
}

WebApiConfig.cs

namespace Klantenzone.API2
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            var jsonFomatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFomatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }
    }
}

AuthContext.cs

namespace Klantenzone.API2.Internal
{
    public class AuthContext : IdentityDbContext<ApplicationUser>
    {
        public AuthContext()
            : base("DefaultConnection")
        {

        }
    }
}

AuthRepository.cs

namespace Klantenzone.API2.Internal
{
    public class AuthRepository : IDisposable
    {
        private AuthContext _ctx;
        private UserManager<ApplicationUser> _userManager;

        public AuthRepository()
        {
            _ctx = new AuthContext();
        }

        public async Task<ApplicationUser> FindUser(string userName, string password)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(_ctx);
            _userManager = new UserManager<ApplicationUser>(userStore);
            //_userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(_ctx));
            //IdentityUser user = await _userManager.FindAsync(userName, password);
            ApplicationUser user2 = await _userManager.FindAsync(userName, password);
            return user2;
        }

        public void Dispose()
        {
            _ctx.Dispose();
            _userManager.Dispose();
        }
    }
}

BeginAPI.cs

[assembly: OwinStartup("APIOwin",typeof(Klantenzone.API2.Internal.BeginAPI))]
namespace Klantenzone.API2.Internal
{
    public class BeginAPI
    {


        public void Configuration(IAppBuilder app)
        {
            ConfigureOAuth(app);
            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            app.UseWebApi(config);
        }

        public void ConfigureOAuth(IAppBuilder app)
        {
            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                Provider = new SimpleAuthorizationServerProvider()
            };

            //Token generation
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        }
    }
}

但是,每次我尝试使用此API登录时(我使用Postman进行测试),我都会收到错误:实体类型ApplicationUser不是当前上下文模型的一部分(在AuthRepository中)。到目前为止,我已尽我所能。 connectionstring不是基于数据库的edmx。我正在使用ApplicationUser,因为AspNetUsers表是由MVC制作的。但它仍然不想工作。谁可以帮助我?我很感激。

0 个答案:

没有答案