Method that works without I call you directly

Asked

Viewed 94 times

0

I’m messing with the Entity Framework and I was mapping to a Many to Many class.

What happens is I used a method I can use inside the DbContext to rename a table, and it works smoothly.

But I was wondering how this method works being that I do not call it anywhere in my program? In case it would be the method OnModelCreating().

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");
        });
    }

}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

  • I believe that you have more curiosity in the way in which this process is implemented and executed. You can check out the EF Core project that is open source directly on github or use some Decompiler dll tool to view the version code you are using. Here is the reference of the Jetbrains dotPeek which is a well-known tool to perform this procedure: https://www.jetbrains.com/decompiler/. LA.

1 answer

2

First you need to understand what is a framework.

You are inheriting from DbContext that the Entity Framework knows what it is and knows there is a method OnModelCreating(). Somewhere within the entire RU infrastructure this method is called when it is suitable. It knows when it needs it.

What it lets you do is write the implementation of this method in its inherited class, so it establishes the virtual and you do it with override.

So when you pass an object EfContext in a place that awaits a DbContext, After all they are compatible, and this is polymorphism, it will call your method and not the method of DbContext. He knows what to call, but does not know what will be executed, you define it. Clever, not?

A different example of this can be seen in What is the difference between using virtual property or not in EF?.

  • Thanks for the reply Maniero, but I really wanted to know how the Entity Framework does it. I will try to give an example to see if I can explain where my doubt really lies. For example, if I create a class of my own, when I instantiate it nothing will be executed unless I have something in my constructor, if I want to call a method of that class I have to call it through the instance. In this case of Efcontext in the constructor I have Database.SetInitializer. I imagined that it could be through this method that this would happen?

  • 1

    You’ve already looked at the source code (https://github.com/aspnet/EntityFramework6)?

Browser other questions tagged

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