不支持每种类型的多个对象集.对象集'ApplicationUsers'和'Users'都可以包含ApplicationUser类型的实例

Fra*_*nva 9 asp.net entity-framework asp.net-mvc-4

我陷入了一个奇怪的问题.

我正在学习MVC 5,几乎所有内容都是由内置模板创建的.我只添加了一堂课历史

public class History
{
    [Key]
    public DateTime Date { get; set; }

    public virtual ApplicationUser User { get; set; }

    public string ItemName { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

在内置的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; }
    }
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

 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 
Run Code Online (Sandbox Code Playgroud)

Jak*_*son 24

以上只是一个解决方法,围绕这个问题有很多答案.

这里

这里

这里

查看IdentityModel.cs内部,您会发现

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
Run Code Online (Sandbox Code Playgroud)

在这种背景下,VS有时会增加 DbSet<ApplicationUser> ApplicationUsers

这是一个重复

IdentityDbContext类的内部是DbSet用户,当您从IdentityContext继承时,继承将该行转换为DbSet<ApplicationUser> User.

修复?取出 DbSet<ApplicationUser> Userpublic class ApplicationDbContext : IdentityDbContext<ApplicationUser>


Joh*_*n S -2

我最终将 ApplicationUser 重命名为 User,重新搭建脚手架,一切都神奇地开始工作。

看起来模板有点奇怪。


归档时间:

查看次数:

10054 次

最近记录:

8 年,1 月 前