Error Multiple object sets per type are not supported

0

I have an error in my application. I am working with Identity for user authentication but calling Controller created is returned the following error:

  

Multiple object sets per type are not supported. The object sets 'Users' and 'Users' can both contain instances of type 'Project01.Areas.Seguranca.Models.Usuario'.

Source error:

  

Source Error:

     

Line 33:   Line 34:   Line 35: @if (Model.Count () == 0)   Line 36: {   Line 37:

But it does not have two contexts of the same type, I have created 2 for the application and the other for the identity .

   namespace Persistencia.Contexts
{
    public class EFContext : DbContext
    {
        public EFContext() 
            : base("EFContext") {
            Database.SetInitializer<EFContext>(
                new MigrateDatabaseToLatestVersion<EFContext, Configuration>());
        }

        protected override void OnModelCreating(
            DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<Categoria> Categorias { get; set; }
        public DbSet<Fabricante> Fabricantes { get; set; }
        public DbSet<Produto> Produtos { get; set; }

    }

}

and the other:

 namespace Projeto01.DAL
{
    public class IdentityDbContextAplicacao : 
        IdentityDbContext<Usuario>
    {
        public static object DataBase { get; private set; }

        public IdentityDbContextAplicacao() : base("IdentityDb")
        {}
        static IdentityDbContextAplicacao()
        {
            Database.SetInitializer<IdentityDbContextAplicacao>
                (new IdentityDbInit());
        }

        public static IdentityDbContextAplicacao Create()
        {
            return new IdentityDbContextAplicacao();
        }

        public DbSet<Usuario> Usuarios { get; set; }
    }
    public class IdentityDbInit: DropCreateDatabaseIfModelChanges
        <IdentityDbContextAplicacao>
    { }
}

View where the error occurs:

 <tbody>
            @if (Model.Count() == 0)
            {
                <tr>
                    <td colspan="3" class="text-center">
                        Sem usuários registrados
                    </td>
                </tr>
            }
            else
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Id)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.UserName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Email)
                        </td>
                        <td>
                            @Html.ActionLink("Alterar", "Edit", new { id = item.Id })
                            |
                            @Html.ActionLink("Detalhes", "Details", new { id = item.Id }) |
                            @Html.ActionLink("Eliminar", "Delete", new { id = item.Id })
                        </td>
                    </tr>
                }
            }
        </tbody>

Can anyone see a solution?

    
asked by anonymous 30.07.2017 / 18:21

2 answers

0

It's probably trying to create the User through the 2 contexts. You should delete the creation within the file generated by Migration. Go in the migration of your application and the file generated when you add / update a migration and take the "User" information from the methods Up and Down (CreateTable, DropTable, etc) that will work.

    
31.07.2017 / 14:13
0

What happens is that IdentityDbContext already sets DbSet automatically and you are setting it again right below.

Your Identity context should look like this

public class IdentityDbContextAplicacao : IdentityDbContext<Usuario>
{
    public static object DataBase { get; private set; }

    public IdentityDbContextAplicacao() : base("IdentityDb") {}

    static IdentityDbContextAplicacao()
    {
        Database.SetInitializer<IdentityDbContextAplicacao>(new IdentityDbInit());
    }

    public static IdentityDbContextAplicacao Create()
    {
        return new IdentityDbContextAplicacao();
    }

    // Remova isto -> public DbSet<Usuario> Usuarios { get; set; }
}
    
11.08.2017 / 21:30