Entity Framework Contexts Error

Asked

Viewed 164 times

1

I’m using two contexts of the Entity Framework, one for the Identity (which is in the cross Cutting layer) and another for 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 "Aspnetusers" table of Identity (and two more fields: name and active) and, with Fluent Api, I configured the class to be mapped to "Aspnetusers", and so I can access the table "Aspnetusers" as if it were an entity of the domain. 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 with the same settings of 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 Identityisolationcontext it tries to create the table "Aspnetusers" and, with this, generates an error because this table already exists. Only I wanted him to update and not try to create another table. And I realized also, that even adding other classes in Identityisolationcontext is not created any table for them and also no field added, but in Applicationdbcontext any change is reflected normally. I didn’t want to put my business entities in the context of Identity.

  • I believe that in the Cross Cutting layer it is not necessary to create the tables again, follow the example: https://github.com/EduardoPires/IdentityIsolation

  • Hello Lucas, I am following this example very, I did not put anything in cross Cutting, only in Infra.Data same, but as said EF launches the problem I mentioned above when I put the entities in the layer of Data..

1 answer

0

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

For some reason, EF cannot 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
}
  • Thank you Nereu, but keep giving the same thing. And also this way would look giant the Onmodelcreating.

Browser other questions tagged

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