Using multiple Schemas with Entity Framework 6

0

I'm starting to work with the Entity Framework, and I need some example of how I can define for my tables the Schemas that each 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;

From this structure, how can I pass to the EntityFramework that the Versao table is defined in Schema [Sis] ?

    
asked by anonymous 07.10.2016 / 21:55

1 answer

2

You can use DataAnnotation Table for this

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 define the default schema in this way

 modelBuilder.HasDefaultSchema("DefaultSchema");
    
07.10.2016 / 21:58