I have read lots of other posts/discussions regarding this error and have not found a clear answer.
我已经阅读了很多关于此错误的其他帖子/讨论,但没有找到明确的答案。
I have customised ASP.NET Identity to store FirstName
and Country
as well as username/password/email address, and I'm now having issues actually storing this within the generated tables within my database.
我已经定制了ASP.NET身份来存储FirstName和Country以及用户名/密码/电子邮件地址,而我现在遇到的问题实际上是在我的数据库中生成的表中存储它。
I have already enabled migrations, added a migration and updated the database to reflect the 2 additional fields within my Identity.
我已经启用了迁移,添加了迁移并更新了数据库以反映我的标识中的2个附加字段。
Error at IdentityResult result = manager.Create(user, model.Password);
IdentityResult的错误结果= 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);
}
}
2 个解决方案
#1
0
I'm thinking it has to do with the nested classes within AuthenticationModels.cs. I'd try moving them outside of the AuthenticationModels
class. Example:
我认为它与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; }
}
}
I just moved everything into the UnderConstruction.Models.AuthenticationModels
namespace, instead of having to create an instance of the AuthenticationModels
class.
我只是将所有内容都移到了UnderConstruction.Models.AuthenticationModels命名空间中,而不必创建AuthenticationModels类的实例。
#2
0
Maybe I've found a solution. Check the UserManager initialization in Startup.Auth.cs to be like this
也许我找到了解决方案。检查Startup.Auth.cs中的UserManager初始化是这样的
UserManagerFactory = () => new UserManager<User>(new UserStore<User>(new MyContext()));
#1
0
I'm thinking it has to do with the nested classes within AuthenticationModels.cs. I'd try moving them outside of the AuthenticationModels
class. Example:
我认为它与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; }
}
}
I just moved everything into the UnderConstruction.Models.AuthenticationModels
namespace, instead of having to create an instance of the AuthenticationModels
class.
我只是将所有内容都移到了UnderConstruction.Models.AuthenticationModels命名空间中,而不必创建AuthenticationModels类的实例。
#2
0
Maybe I've found a solution. Check the UserManager initialization in Startup.Auth.cs to be like this
也许我找到了解决方案。检查Startup.Auth.cs中的UserManager初始化是这样的
UserManagerFactory = () => new UserManager<User>(new UserStore<User>(new MyContext()));