Working with view in a context in Entity Framework 6

0

Speak Personal All right !? I have a problem that I would like to be helped.

Well I have a simple application with 4 related tables

I'musingtheentityframework6,myproblemisintheUSERSclass,becauseinrealityitisaVIEW,fromanotherdatabase,researchingthisIsawthatIcancreateanothercontext(readonly)Move View - Entity Framework to see this view, but my problem is that in other classes I need to have the relationship with the table How can I do this? When I include it in my main context the system tries to create the users table but it gives an error because the view already exists in the database.

Below is a snippet of my code:

    public class BancoContexto: DbContext
{
    public BancoContexto(): base("conexao")
    {
        this.Configuration.LazyLoadingEnabled = true;

    }
    public DbSet<LinhaProducao> LinhaProducao { get; set; }
    public DbSet<AreaResponsavel> AreaResponsavel { get; set; }
    public DbSet<Cartao> Cartao { get; set; }
    public virtual DbSet<USUARIOS> USUARIOS { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AreaResponsavel>().ToTable("AreaResponsavel");
        modelBuilder.Entity<LinhaProducao>().ToTable("LinhaProducao");
        modelBuilder.Entity<Cartao>().ToTable("Cartao");

        //aqui é o meu problema
        modelBuilder.Entity<USUARIOS>().ToTable("USUARIOS");



        #endregion

        base.OnModelCreating(modelBuilder);
    }
}

public class AreaResponsavel
{
    [Key]
    public int AreaResponsavelID { get; set; }
    public string NomeArea { get; set; }
    public int UsuarioID { get; set; }
    public virtual USUARIOS Responsavel { get; set; }
}
public class Cartao
{
    public int CartaoID { get; set; }
    public int NroCartao { get; set; }
    public int OrdemProducao { get; set; }
    public string Modelo { get; set; }
    public string CodigoPeca { get; set; }
    public string DescricaoProblema { get; set; }
    public DateTime Data { get; set; }
    public DateTime DataHoraInicio { get; set; }
    public DateTime DataHoraFim { get; set; }
    public int UsuarioID { get; set; }
    public USUARIOS Colaborador { get; set; }
    public int LinhaProducaoID { get; set; }
    public LinhaProducao Linha { get; set; }
    public int MaquinasAfetadas { get; set; }
    public int AreaResponsavelID { get; set; }
    public AreaResponsavel Area { get; set; }
    public int LiderID { get; set; }
    public USUARIOS Lider { get; set; }
    public bool Gemba { get; set; }
}
public class LinhaProducao
{
    public int LinhaProducaoID { get; set; }
    public string NomeLinha { get; set; }
    public int UsuarioID { get; set; }
    public USUARIOS Responsavel { get; set; }
}

See that in the classes above I have a relationship with the User class !!!

Thanks in advance for your help!

    
asked by anonymous 10.11.2016 / 21:07

1 answer

1

You can disable automatic database change, with the following code in the constructor of your context class:

Database.SetInitializer (null);

This way, it will no longer try to create the table, but then you will have to manually apply the changes.

I would consider using migrations, this way you determine when the changes in the database will be generated, migrations can even generate the scripts ready for you, giving you the chance to even apply the scripts, without running the risk of the tool doing any barbering in your database, then you avoid creating the table that is actually a view.

    
10.11.2016 / 23:46