Using Multiple Schemas with Entity Framework 6

Asked

Viewed 157 times

0

I am starting to work with the Entity Framework, and I need some example of how I can define for my tables the Schema’s that each one is contained. My database was created as in the example:

/*schema [Pes] -> relativo ao contexto Pessoa*/
Pes.Pessoa; 
Pes.PessoaFisica; 
Pes.EstadoCivil;  

/*schema [Sis] -> relativo ao contexto do Sistema*/
Sis.Versao;
Sis.Configuracao;
Sis.Usuario;

Starting from this structure, how can I pass to the Entityframework that table Versao defined in the Schema [Sis]?

1 answer

2


You can use the Dataannotation Table for that reason

using System.ComponentModel.DataAnnotations.Schema;

[Table("Pes.Pessoa")]
public class Pessoa
{ ... }

Or, if you use Fluentapi, you can do it this way

public class Contexto : DbContext
{
    public DbSet<Pessoa> Pessoas { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Pessoa>().ToTable("Pessoa", "Pes");            
    }
}

You can also set the schema standard, thus

 modelBuilder.HasDefaultSchema("DefaultSchema");
  • like Dataannotation, I working this way is declared directly in class only, I do not need to specify anything else in Dbcontext?

  • 2

    Nope. Nothing more.

Browser other questions tagged

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