Working with view in context in Entity Framework 6

Asked

Viewed 565 times

0

It’s all right, guys? I have a certain problem I’d like you to help me.

Good I have a simple application with 4 related tables

Classes

I am using Entity framework 6, my problem is in the USUARIOS class, because in reality it is a VIEW, from another database, searching about it I saw that I can create another context(readonly) Map View - Entity Framework to consult this view, but my problem is that in other classes I need to have relationship with the user table, how can I do this? When I include it in my main context the system tries to create the table users but gives an error because the view already exists in the database.

Below an excerpt from 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 above classes I have the relationship with the User class!!!

Thanks for your help!

1 answer

1

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

Database.Setinitializer(null);

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

I would consider using Migrations, so 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 apply the scripts yourself, without running the risk of the tool doing some shaving on your bank, hence you avoid creating the table which is actually a view.

  • Thank you for the reply Fernando! Well I’m actually using Code First, but I would like to avoid manually making the scripts, I wonder if there is any way to make the system recognize that entity as a view in the bank and not as a table...

  • Oh sorry, I thought it was model-first due to the images of the class diagram.

  • I will edit the answer, in fact I believe that there is no way to do what you want, I searched, I use views here, but not with automatic generation of tables. As I said, Migrations generates everything for you, including, applies the changes, the difference is that you have to run a command in vs asking to generate context changes on a given basis.

Browser other questions tagged

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