2
I know that to return the values of a directly related entity in the bank is done so with LINQ:
List<Usuario> lista = banco.Usuario.Include("Tipo").ToList();
But how can I get the data from another table related to the "Type" table? For example, the table "Type" has a relationship with the table "Situation" to which the table "User" does not know.
Summarizing the relationship is like this: "User" with "Type" and "Type" with "Situation". I need the data from "Situation" coming along also.
Dbcontext
public partial class BancoDeHorasEntities : DbContext
{
public BancoDeHorasEntities()
: base("name=BancoDeHorasEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Usuario> Usuario { get; set; }
public DbSet<Tipo> Tipo { get; set; }
public DbSet<Situacao> Situacao { get; set; }
public DbSet<sysdiagrams> sysdiagrams { get; set; }
}
Usuario.Cs
public partial class Usuario
{
public int Id { get; set; }
public Nullable<int> IdPessoa { get; set; }
public string Descricao { get; set; }
public Nullable<int> IdTipo { get; set; }
public virtual Tipo Tipo { get; set; }
}
Tipo.Cs
public partial class Tipo
{
public Tipo()
{
this.Usuarios = new HashSet<Usuario>();
}
public int Id { get; set; }
public Nullable<int> IdSituacao { get; set; }
public string Descricao { get; set; }
public virtual Situacao Situacao { get; set; }
public virtual ICollection<Usuario> Usuarios { get; set; }
}
Situation.Cs
public partial class Situacao
{
public Situacao()
{
this.Tipos = new HashSet<Tipo>();
}
public int Id { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public virtual ICollection<Tipo> Tipo { get; set; }
}
Modeling of the Database
In short the problem is: how to obtain, also, the data from the table "Situation" when searching "User".
Why his
OnModelCreating
fires aUnintentionalCodeFirstException
?– Leonel Sanches da Silva
I don’t really know. The project was created by another and when I started using it it was like this.
– Luídne