我被一个奇怪的问题卡住了。
我正在学习MVC 5,几乎一切都是由内置模板创建的。我只添加了一个类History
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:WorkplaceMyOnlineShoppingMyOnlineShoppingControllersAccountController.cs Line: 126
以上只是一个解决方法,在SO周围有许多相同问题的答案。
这里这里和
查看IdentityModel.cs,你会发现
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
在这个上下文中,VS有时会添加DbSet<ApplicationUser> ApplicationUsers
这是重复的
在IdentityDbContext类的内部是一个DbSet User,当你从IdentityContext子类化时,继承将那一行转换为DbSet<ApplicationUser> User
。
修复吗?从public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
中移除DbSet<ApplicationUser> User
当您的ApplicationDbContext
类更改时发生此错误。只需要在Models文件夹中做这个操作,然后进入IdentityModels.cs
类并删除最后一行
public System.Data.Entity.DbSet < Project.Models.ApplicationUserProject.Models.ApplicationUser> ApplicationUsers { get; set; }
这一行是由scaffold自动生成的。
我最终将ApplicationUser重命名为User,重新脚手架,一切都神奇地开始工作。
看起来模板有点不正常