我遇到了一个奇怪的问题。
我正在学习MVC 5,几乎所有内容都是由内置模板创建的。 我只添加了一个类历史
public class History
{
[Key]
public DateTime Date { get; set; }
public virtual ApplicationUser User { get; set; }
public string ItemName { get; set; }
}
在内置的ApplicationUser中:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual ICollection<History> Histories { get; set; }
}
以下是错误消息:
Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyOnlineShopping.Models.ApplicationUser'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyOnlineShopping.Models.ApplicationUser'.
Source Error:
Line 124: {
Line 125: var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
Line 126: IdentityResult result = await UserManager.CreateAsync(user, model.Password);
Line 127: if (result.Succeeded)
Line 128: {
Source File: f:\Workplace\MyOnlineShopping\MyOnlineShopping\Controllers\AccountController.cs Line: 126
答案 0 :(得分:22)
以上只是一个解决方法,围绕这个问题有很多答案。
和Here
查看IdentityModel.cs,你会发现
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
在此上下文中,VS有时会添加DbSet<ApplicationUser> ApplicationUsers
这是一个重复的
IdentityDbContext类的内部是DbSet用户,当您从IdentityContext继承时,继承会将该行转换为DbSet<ApplicationUser> User
。
修复?从DbSet<ApplicationUser> User
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
答案 1 :(得分:0)
更改您的ApplicationDbContext
类时,会发生此错误。只需在“模型”文件夹中执行此操作,然后转到IdentityModels.cs
类并删除最后一行
public System.Data.Entity.DbSet < Project.Models.ApplicationUserProject.Models.ApplicationUser> ApplicationUsers { get; set; }
此行是由脚手架自动生成的。
答案 2 :(得分:-3)
我最终将ApplicationUser重命名为User,重新加载,一切都神奇地开始工作。
看起来模板有点不合时宜了。