Error: Multiple object sets per type are not supported

浪子不回头ぞ 提交于 2019-12-11 02:03:56

问题


Here is details of the error:

There was an error running the selected code generator: 'Unable to retrive metadata for 'Models.ApplicationUser'. Multiple object sets per type are not supported. The objects sets 'ApplicationUsers' and 'Users' can both contain instances of type 'Models.ApplicationUser'.

I am getting this while scaffolding a view in MVC project. I got the same error if i try to scafford a controller. I have seen a few of this error on SO but it seems those don't apply to my case.

Here is my DbContext. It is just the one comes with default MVC projects with few more DbSets. I don't have any DbSet for ApplicationUsers or Users or anything like that on my context or anywhere in the project. But why am i getting this error?

public class ApplicationUser : IdentityUser
{        
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {            
    }

    public virtual DbSet<ItemType> ItemTypes { get; set; }
    public virtual DbSet<Item> Items { get; set; }
    public virtual DbSet<Category> Categories { get; set; }
    public virtual DbSet<List> Lists { get; set; }
    public virtual DbSet<Level> Levels { get; set; }        
}

Here is my List class. I suspect this error has something to do with it. But why?

public class List
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }        
    public string UserId { get; set; }

    public virtual ICollection<Item> Items { get; set; }
    public virtual ApplicationUser User { get; set; }
}

This is the only place that has reference to ApplicationUser in all entities. One more question, a bit off topic is 'was it bad decision to extend IdentityDbContext rather than creating a new DbContext in the application?

Environment: MVC5, EF6 Code First, VS2013 Ultimate, C#.


回答1:


i have been facing same issue.

when i tried to get all Application users in my code, it automatically created following property in ApplicationDbContext class:

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

please remove this line and all will be fine.




回答2:


I think this is you answer

public class ApplicationUser : IdentityUser
{
        public virtual ICollection<List> Lists{ get; set; }
}

you have to give the user class your Icollection



来源:https://stackoverflow.com/questions/23409644/error-multiple-object-sets-per-type-are-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!