实体类型ApplicationUser不是当前上下文模型的一部分。

时间:2018-12-16 10:51:39

标签: asp.net-mvc asp.net-identity

请告诉我我的工作在哪里错误

我从一开始就没有身份设计和编码项目。后来,在创建程序时,我需要为项目添加身份。 我在程序的数据库中创建了身份表。 当我加载http://localhost/account/login并在“登录”视图中输入电子邮件和密码,然后单击“确定”按钮时,،会遇到以下错误。此外,在我要注册用户时也会发生此错误。Screenshot of error 我还向web.config添加了以下连接:

<connectionStrings>
<add name="DefaultConnection" connectionString="metadata=res://*/Models.DomainModels.MachinaryModel.csdl|res://*/Models.DomainModels.MachinaryModel.ssdl|res://*/Models.DomainModels.MachinaryModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS2017;initial catalog=Machinary;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="MachinaryEntities" connectionString="metadata=res://*/Models.DomainModels.MachinaryModel.csdl|res://*/Models.DomainModels.MachinaryModel.ssdl|res://*/Models.DomainModels.MachinaryModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS2017;initial catalog=Machinary;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

帐户管理员/登录:

 Public Async Function Login(model As LoginViewModel, returnUrl As String) As Task(Of ActionResult)
        If Not ModelState.IsValid Then
            Return View(model)
        End If

        ' This doesn't count login failures towards account lockout
        ' To enable password failures to trigger account lockout, change to shouldLockout := True
        Dim result = Await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout:=False)
        Select Case result
            Case SignInStatus.Success
                Return RedirectToLocal(returnUrl)
            Case SignInStatus.LockedOut
                Return View("Lockout")
            Case SignInStatus.RequiresVerification
                Return RedirectToAction("SendCode", New With {
                    returnUrl,
                    model.RememberMe
                })
            Case Else
                ModelState.AddModelError("", "Invalid login attempt.")
                Return View(model)
        End Select
    End Function

IdentifyModels:

Imports System.Security.Claims
Imports System.Threading.Tasks
Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.AspNet.Identity.Owin

' You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
Public Class ApplicationUser
    Inherits IdentityUser
    Public Async Function GenerateUserIdentityAsync(manager As UserManager(Of ApplicationUser)) As Task(Of ClaimsIdentity)
        ' Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        Dim userIdentity = Await manager.CreateIdentityAsync(Me, DefaultAuthenticationTypes.ApplicationCookie)
        ' Add custom user claims here
        Return userIdentity
    End Function
End Class

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("DefaultConnection", throwIfV1Schema:=False)
    End Sub

    Public Shared Function Create() As ApplicationDbContext
        Return New ApplicationDbContext()
    End Function
End Class

startup.Auth.vb:

Partial Public Class Startup
    ' For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    Public Sub ConfigureAuth(ByVal app As IAppBuilder)
        app.CreatePerOwinContext(AddressOf ApplicationDbContext.Create)
        app.CreatePerOwinContext(Of ApplicationUserManager)(AddressOf ApplicationUserManager.Create)
        app.CreatePerOwinContext(Of ApplicationSignInManager)(AddressOf ApplicationSignInManager.Create)
        ' Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(New CookieAuthenticationOptions With {
.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
.LoginPath = New PathString("/Account/Login")
})


        ' Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie)

        ' Uncomment the following lines to enable logging in with third party login providers
        'app.UseMicrosoftAccountAuthentication(
        ' clientId: "",
        ' clientSecret: "");

        'app.UseTwitterAuthentication(
        ' consumerKey: "",
        ' consumerSecret: "");

        'app.UseFacebookAuthentication(
        ' appId: "",
        ' appSecret: "");

        'app.UseGoogleAuthentication();
    End Sub

End Class

0 个答案:

没有答案