Multiple error Object sets per type are not supported

Asked

Viewed 706 times

1

I have a bug in my application. I am working with Identity for user authentication however when calling Controller created is returned me the following error:

Multiple Object sets per type are not supported. The Object sets 'Usuarios' and 'Users' can Both contain instances of type 'Project01.Areasc.Security.Models.Usuario'.

Error of origin:

Error of Origin:

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

However it does not have two contexts of the same type, I created 2 for the application and another 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 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 any solution?

2 answers

1

What happens is that the IdentityDbContext already arrow the 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; }
}

0

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

  • In the Migrations creation file there is nothing related to Users, only my other 3 tables, Products, Manufacturers and Categories...

  • you could post the class where the error is happening, then?

  • The error occurs in the Index view, posted in the initial topic, the section where the error is given..

  • But only the line that generates the error doesn’t help much. The interesting thing would be to show the whole context of your call. I would indicate you post your entire view, the controller that calls the view, your typed object on the screen would also be interesting. If the application runs, then the problem lies in some logic or modeling. At first I thought the error was time to try to create/update your bank with the Fluent API.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.