
[英]entity type ApplicationUser is not part of the model for the current context
[英]The entity type ApplicationUser is not part of the model for the current context
我已经建立了一个新的asp.net网络表单应用程序。 我想使用我自己的实体框架数据模型,使用提供的owin身份验证创建登录并注册功能。
我已更改了默认值和IdentityModels.cs中的连接字符串,但是使用登录名时出现上述错误。
代码与默认值保持不变,当我将ApplicationDBContext更改回DefaultConnection时,它可以按预期工作。
我可以使用linq查询来检查输入的电子邮件和密码,因此可以从数据库访问数据。
在登录用户控件内的以下行上引发错误:
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
除了提供的DefaultConnection之外,我还需要做些其他事情以使OWIN身份验证与数据库一起工作吗?
代码段:
生成的寄存器:
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
生成的登录名:
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
case SignInStatus.LockedOut:
Response.Redirect("/Account/Lockout");
break;
case SignInStatus.RequiresVerification:
Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
Request.QueryString["ReturnUrl"],
RememberMe.Checked),
true);
break;
case SignInStatus.Failure:
default:
FailureText.Text = "Invalid login attempt";
ErrorMessage.Visible = true;
break;
}
Linq查询以查找用户:
var myUser = db.Dat_Account
.FirstOrDefault(u => u.UserName == Email.Text
&& u.Password == Password.Text);
生成的ApplicationUser
public class ApplicationUser : IdentityUser
{
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
return Task.FromResult(GenerateUserIdentity(manager));
}
}
我的用户表中除了用户名,电子邮件和密码外,还有更多字段。 这有可能引起问题吗?
语境:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyDBEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
看起来您没有数据库初始化程序,因此默认行为是CreateDatabaseIfNotExists。 因此,要么删除现有数据库,然后让EF使用身份信息创建它,要么在构造函数中将初始化程序切换为DropCreateDatabaseWhenModelChanges:
static ApplicationDbContext() {
Database.SetInitializer(new DropCreateDatabaseWhenModelChanges<ApplicationDbContext>());
}
一旦启动并运行并且拥有不想丢失的数据,则应关闭初始化程序(Database.SetInitializer(new NullDatabaseInitializer());)或使用迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.