我已阅读了很多有关此错误的其他帖子/讨论,但未找到明确答案。
我已经自定义了ASP.NET身份来存储FirstName
和Country
以及用户名/密码/电子邮件地址,而我现在在生成的表中实际存储了这个问题我的数据库。
我已启用迁移,添加了迁移并更新了数据库以反映我的身份中的2个附加字段。
IdentityResult result = manager.Create(user, model.Password);
$exception {"The entity type ApplicationUser is not part of the model for the current context."} System.Exception {System.InvalidOperationException}
的web.config:
<connectionStrings>
<remove name="DefaultConnection" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=WIN8-SUNEETAGU\SQLSERVER;Initial Catalog=UnderConstruction;Integrated Security=True" />
</connectionStrings>
AuthenticationModels.cs
namespace UnderConstruction.Models
{
public class AuthenticationModels
{
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string Country { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<UnderConstruction.Models.AuthenticationModels.ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{}
}
public class RegisterModel
{
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
public string ConfirmPassword { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessage="Please enter a valid email address.")]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Country")]
public string Country { get; set; }
}
}
}
AuthenticationController.cs
public class AuthenticationController : Controller
{
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(AuthenticationModels.RegisterModel model)
{
if (ModelState.IsValid)
{
try
{
var userStore = new UserStore<UnderConstruction.Models.AuthenticationModels.ApplicationUser>();
var manager = new UserManager<UnderConstruction.Models.AuthenticationModels.ApplicationUser>(userStore);
var user = new UnderConstruction.Models.AuthenticationModels.ApplicationUser() { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, Country = model.Country };
IdentityResult result = manager.Create(user, model.Password);
return View("RegisterSuccessful");
}
catch (Exception e)
{
ModelState.AddModelError("", e);
}
}
return View("Register",model);
}
}
答案 0 :(得分:0)
我认为它与AuthenticationModels.cs中的嵌套类有关。我会尝试将它们移到AuthenticationModels
课程之外。例如:
namespace UnderConstruction.Models.AuthenticationModels
{
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string Country { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<UnderConstruction.Models.AuthenticationModels.ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{}
}
public class RegisterModel
{
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
public string ConfirmPassword { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessage="Please enter a valid email address.")]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Country")]
public string Country { get; set; }
}
}
我只是将所有内容都移到UnderConstruction.Models.AuthenticationModels
命名空间中,而不必创建AuthenticationModels
类的实例。
答案 1 :(得分:0)
也许我找到了解决方案。 检查Startup.Auth.cs中的UserManager初始化是否像这样
UserManagerFactory = () => new UserManager<User>(new UserStore<User>(new MyContext()));