Error between Entity Framework Contexts

1

I'm using two Entity Framework contexts, one for the Identity (which is in the cross cutting layer) and another to my application (in the Data layer), both point to the same database.

Date Layer:

public class IdentityIsolationContext : DbContext
{
    public IdentityIsolationContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Usuario> Usuarios { get; set; } //Fala que já existe essa tabela, mas eu gostaria que atualizasse...
    public DbSet<Produto> Produtos { get; set; } // Não gera a tabela
    public DbSet<Categoria> Categorias { get; set; } // Não gera a tabela

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UsuarioConfig());
        modelBuilder.Configurations.Add(new ProdutoConfig());
        modelBuilder.Configurations.Add(new CategoriaConfig());

        base.OnModelCreating(modelBuilder);
    }
}

Cross Cutting Layer:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IDisposable
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Usuario> Usuarios { get; set; } 
    public DbSet<Produto> Produtos { get; set; }
    public DbSet<Categoria> Categorias { get; set; }

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

In my domain I created a User class with the same fields of the Identity table "AspNetUsers" (and two more fields: name and active) and, with Fluent Api, I configured the class to be mapped to "AspNetUsers", so I can access the "AspNetUsers" table as if it were a domain entity . Below the settings:

public class Usuario
{
    public int Id { get; set; }

    public string Nome { get; set; } // Novo campo

    public bool Ativo { get; set; } // Novo campo

    public virtual string Email { get; set; }

    public virtual bool EmailConfirmed { get; set; }

    public virtual string PasswordHash { get; set; }

    public virtual string SecurityStamp { get; set; }

    public virtual string PhoneNumber { get; set; }

    public virtual bool PhoneNumberConfirmed { get; set; }

    public virtual bool TwoFactorEnabled { get; set; }

    public virtual DateTime? LockoutEndDateUtc { get; set; }

    public virtual bool LockoutEnabled { get; set; }

    public virtual int AccessFailedCount { get; set; }

    public virtual string UserName { get; set; }
}

I tried to leave the same settings as the original table:

public class UsuarioConfig : EntityTypeConfiguration<Usuario>
{
    public UsuarioConfig()
    {
        HasKey(u => u.Id);

        Property(u => u.Nome)
            .IsRequired()
            .HasMaxLength(100);

        Property(u => u.Email)
            .IsRequired()
            .HasMaxLength(256);

        Property(u => u.UserName)
            .IsRequired()
            .HasMaxLength(256);

        ToTable("AspNetUsers");
    }
}

But when giving an Update-Database in the IdentityIsolationContext it tries to create the table "AspNetUsers" and, with this, it generates an error because this table already exists. But I wanted him to update and not try to create another table. And I also realized that even adding other classes in the IdentityIsolationContext creates no table for them and also no fields added, but in the ApplicationDbContext any change is reflected normally. I did not want to put the entities of my business in the context of Identity.

    
asked by anonymous 17.10.2016 / 00:28

1 answer

0

I had this problem a few years ago. As I recall, the solution was to do the "mapping" directly in DbContext, using the OnModelCreating method.

For some reason, EF can not do this when "mapping" is in an "external" class.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<User>().ToTable("User");


            #region User
            modelBuilder.Entity<User>().Property(u => u.Id)
                .HasColumnOrder(10)
                .IsRequired()
                .HasColumnType("varchar")
                .HasMaxLength(128);

            modelBuilder.Entity<User>().Property(u => u.AccessFailedCount)
                .HasColumnOrder(20);

            modelBuilder.Entity<User>().Property(u => u.Active)
                .HasColumnOrder(30);

            modelBuilder.Entity<User>().Property(u => u.Email)
                .HasColumnOrder(50)
                .IsRequired()
                .HasColumnType("varchar")
                .HasMaxLength(256);

            modelBuilder.Entity<User>().Property(u => u.EmailConfirmed)
                .IsRequired()
                .HasColumnOrder(60);

            modelBuilder.Entity<User>().Property(u => u.LockoutEnabled)
                .HasColumnOrder(70);

            modelBuilder.Entity<User>().Property(u => u.LockoutEndDateUtc)
                .HasColumnOrder(80);

            modelBuilder.Entity<User>().Property(u => u.Name)
                .HasColumnOrder(90)
                .IsRequired()
                .HasColumnType("varchar")
                .HasMaxLength(256);


            modelBuilder.Entity<User>().Property(u => u.PasswordHash)
                .HasColumnOrder(100);

            modelBuilder.Entity<User>().Property(u => u.PhoneNumber)
                .HasColumnOrder(110)
                .HasColumnType("varchar")
                .HasMaxLength(15);

            modelBuilder.Entity<User>().Property(u => u.PhoneNumberConfirmed)
                .HasColumnOrder(120);

            modelBuilder.Entity<User>().Property(u => u.SecurityStamp)
                .HasColumnOrder(130);

            modelBuilder.Entity<User>().Property(u => u.TwoFactorEnabled)
                .HasColumnOrder(140);

            modelBuilder.Entity<User>().Property(u => u.UserName)
                .HasColumnOrder(150)
                .IsRequired()
                .HasColumnType("varchar")
                .HasMaxLength(256)
                .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IdxUserName") { IsUnique = true }));



            #endregion
}
    
17.10.2016 / 02:46