I'm trying to return a list of objects generated by the DataBase First of the Entity Framework but I get this error:
The entity or complex type 'leaosites04Model.TB_LEMBRETES' can not be constructed in a LINQ to Entities query.
I'm trying to somehow not create a class for each different type of query I make with different columns in EF ... The method looks like this:
public List<TB_LEMBRETES> getLembretes(int? situacao)
{
try
{
using (dbEmpEntities db = new dbEmpEntities())
{
List<TB_LEMBRETES> result =
(from l in db.TB_LEMBRETES
join c in db.TB_CLIENTE on l.TB_CLIENTE.id_cliente equals c.id_cliente
where l.situacao == 0
select new TB_LEMBRETES
{
dt_lembrete = l.dt_lembrete,
obs = l.obs,
TB_CLIENTE = new TB_CLIENTE() { id_cliente = c.id_cliente, nome = c.nome }
}).ToList();
return result;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
TB_LEBBRETE and TB_CLIENTE are models, within Model1.edmx (within Model1.tt), which were generated by Entity at the time of Database First.
I have seen on the net in some places that this is not possible, but I did not understand why!
Below are the models:
[Table("leaosites04.TB_LEMBRETES")]
public partial class TB_LEMBRETES
{
[Key]
public int id_lembrete { get; set; }
public int? id_cliente { get; set; }
public DateTime? dt_lembrete { get; set; }
[Column(TypeName = "text")]
[StringLength(65535)]
public string obs { get; set; }
public virtual TB_CLIENTE TB_CLIENTE { get; set; }
}
[Table("leaosites04.TB_CLIENTE")]
public partial class TB_CLIENTE
{
public TB_CLIENTE()
{
TB_LEMBRETES = new HashSet<TB_LEMBRETES>();
TB_TELEFONES = new HashSet<TB_TELEFONES>();
}
[Key]
public int id_cliente { get; set; }
[Required]
[StringLength(100)]
public string nome { get; set; }
public virtual ICollection<TB_LEMBRETES> TB_LEMBRETES { get; set; }
}