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

9

我遇到了一个奇怪的问题。

我正在学习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:\Workplace\MyOnlineShopping\MyOnlineShopping\Controllers\AccountController.cs    Line: 126 

我遇到了同样的问题。在我的情况下,一切都很正常,直到我向控制器添加了一个操作,然后添加了一个视图到该操作中。我已经删除了该操作和视图,但仍然出现错误。(这是我知道添加的唯一两件事) - John S
1
嗨@JohnS,我想我找到了这个问题的“表面答案”。我发现似乎EntityFramework在ApplicationUser下自动添加了一个IdentityUser集合。我将其删除后,问题解决了。希望对你有所帮助。 - Franva
3个回答

24

以上只是一种解决方法,关于同样的问题在Stack Overflow上还有很多其他答案。

这里

这里

以及这里

在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

为什么你要将“THIS IS A DUPLICATE”加粗? - Russia Must Remove Putin
为了强调重复的DbSet位于该位置,请添加强调。 - Jake Garrison
嗨,杰克,非常感谢。即使是4个月前的代码,我仍然能记得一点。我记得你在我的项目中提到的代码。我会尝试一下。谢谢。 - Franva
不客气!希望其他搜索这个问题的人也会发现我的答案有帮助。谢谢。 - Jake Garrison
你刚刚拯救了我剩下的头发,让它不再被我拔光。在我给ApplicationUser类添加一些自定义属性后,这个问题突然出现了,但没有任何线索。谢谢! :) - iCollect.it Ltd
我忘了每次使用脚手架(添加视图)创建以ApplicationUser为模型的列表视图时,它是如何将该dbset添加到您在帖子@Jake Garrison中提到的公共类中的,这使我今晚免于用头撞墙。 - nocturns2

0

这个错误是由于你的 ApplicationDbContext 类发生了变化。只需在 Models 文件夹中执行以下操作,然后转到 IdentityModels.cs 类并删除最后一行即可。

public System.Data.Entity.DbSet < Project.Models.ApplicationUserProject.Models.ApplicationUser> ApplicationUsers { get; set; }

这行代码是由脚手架自动生成的。


-2
我最终将ApplicationUser重命名为User,重新生成了代码,一切就像魔术般地开始工作了。
看起来模板有点不协调。

嗨John,我已将ApplicationUser重命名为User(也没有起作用),但没有重新生成。你是如何重新生成的? - Franva
1
我通过从使用User类的操作创建另一个视图来触发重新架构。我相信有更好或更直接的方法,但我似乎找不到那个菜单选项,所以我选择了这种方式。 - John S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接