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
– Lucas Lopes
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..
– Raphael