I'm a newbie in MVC apps, and I've encountered a very specific problem. Thing is, I have 2 classes: "Paciente" and "Analises".

public class Paciente
{
    public virtual Guid PacienteID { get; set; }
    public virtual string Nome { get; set; }
    public virtual string Sexo { get; set; }
    public virtual DateTime DataDeNasc { get; set; }
    public virtual int IdadeDiag { get; set; }

    public virtual IList<Analises> analises { get; set; } 
}


public class Analises
{
    public virtual Guid AnaliseID { get; set; } 
     // some analysiss values
    public virtual decimal afp { get; set; }
    public virtual decimal hemoglobina { get; set; }
    public virtual DateTime DataDaAnalise { get; set; }
    public virtual Paciente pac { get; set; } 

}

So, a Patient has many Analysis, and each Analysis has one Patient (on-to-many).

I have mapped this with NHIbernate and FluentNHibernate:

public PacienteMap()
    {
        Table("pacientes");
        Id(x => x.PacienteID).GeneratedBy.Guid();
        Map(x => x.Nome);
        Map(x => x.Sexo);
        Map(x => x.DataDeNasc).CustomType("Date");;
        Map(x => x.IdadeDiag);
        HasMany(m => m.analises).Not.KeyNullable().Fetch.Join().KeyColumn("pacienteid"); 
    }



 public AnalisesMap()
    {
        Table("analises");
        Id(x => x.AnaliseID).GeneratedBy.Guid();
        Map(x => x.afp);
        Map(x => x.hemoglobina);
        Map(x => x.DataDaAnalise).CustomType("Date");
        References(x => x.pac).ForeignKey("pacienteid").Not.Nullable();
    }

My problem is that I'm using jTable to show this. I want to see a list of Patients (and it works), and then, a list of analysis for each patient (doesn't work!)

My Controller goes like this:

 [HttpPost]
    public JsonResult AnalisesList(Guid pacienteId)
    {
        try
        {

            var list_analises = AnalisesRepository.GetPacienteAnalises(pacienteId);   
            var all_analises = Mapper.Map<IEnumerable<Analises>, IEnumerable<AnalisesView>>(list_analises);
            List<AnalisesView> analises = all_analises.ToList();
            return Json(new { Result = "OK", Records = analises });

        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message });
        }
    }

I am also using automapper to go from the Object Views from Database Objects.

So, rigth now, my analysis list does not show! I don't know why. The GetPacienteAnalises is like:

 public IList<T> GetPacienteAnalises (Guid pacienteId)
    {
        using (ISession session = SessionManager.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {

                return session.CreateCriteria(typeof(T)).Add(Restrictions.Eq("pacienteid", pacienteId)).List<T>();

            }
        }  
    }

So, I think everything is Okay... But I keep receiving a "could not resolve property: pacienteid of: Infraestrutura.Models.Analises".

有帮助吗?

解决方案

Your problem is in this line:

return session.CreateCriteria(typeof(T))
    .Add(Restrictions.Eq("pacienteid", pacienteId))
    .List<T>();

I assume that T in this case is Analises. Analises does not have a property named pacienteid. Instead, it has a property named pac. Replace "pacienteid" with "pac.PacienteID". The fields that you use when creating the criteria should be the property names, not the column names.

return session.CreateCriteria(typeof(T))
    .Add(Restrictions.Eq("pac.PacienteID", pacienteId))
    .List<T>();

If you were filtering by some column other than Paciente's primary key, Nome for example, then a join would be necessary. In that case, you would do something like:

session.CreateCriteria(typeof(T))
    .CreateAlias("pac", "p")            // CreateAlias performs an inner join
    .Add(Expression.Eq("p.Nome", name))
    .List<T>()

But no join is needed to get to pac.PacienteID.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top