I'm messing with the Entity Framework and I was doing mapping for a Many to Many class.
What happens is that I used a method that I can use within DbContext
to rename a table, and it works without problems.
But I was wondering how this method works, since I do not call it anywhere in my program? In this case it would be the OnModelCreating()
method.
class EfContext : DbContext
{
public DbSet<Editora> EditorasDbSet { get; set; }
public DbSet<Cliente> ClientesDbSet { get; set; }
public DbSet<Estado> EstadosDbSet { get; set; }
public DbSet<Governador> GovernadorsDbSet { get; set; }
public DbSet<Departamento> DepartamentosDbSet { get; set; }
public DbSet<Funcionario> FuncionariosDbSet { get; set; }
public DbSet<Pedido> PedidosDbSet { get; set; }
public DbSet<Consumidor> ConsumidoresDbSet { get; set; }
public DbSet<Autor> AutorsDbSet { get; set; }
public DbSet<Livro> LivrosDbSet { get; set; }
public EfContext()
{
CustomDBInitializer initializer = new CustomDBInitializer();
Database.SetInitializer<EfContext>(initializer);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Autor>().HasMany(autor => autor.Livros).WithMany(livro => livro.Autores).Map( x =>
{
x.ToTable("livros_e_autores");
x.MapLeftKey("autor_id");
x.MapRightKey("livro_id");
});
}
}