Problem retrieving References from a query in Nhibernate HasMany

0

I'm using Nhibernate with WebAPI I have 2 entities Profile and personLogin Personalogin may have N Profile and Profile 1 Person Login. However, when I go to the webservice PessoaLogin, the Profile reference only works if it is NULL if I add a Profile, whenever you go to PersonLogin's Error. I will describe the code here

Profile.cs

  public class Perfil
{



    public Perfil()
    {

        PessoaLogin = new Collection<PessoaLogin>();

    } 

    public virtual int IdPerfil { get; set; }
    public virtual string Descricao { get; set; }
    public virtual ICollection<PessoaLogin> PessoaLogin { get; set; }
}

ProfileMap.cs

  public class PerfilMap : ClassMap<Perfil>
{
    public PerfilMap()
    {
        Id(x => x.IdPerfil);

        Map(x => x.Descricao)
        .Not.Nullable()
            .Length(MapLength.TextoCurto);

        HasMany(x => x.PessoaLogin)
            .Not.LazyLoad()
            .KeyColumn("Id_Perfil");



        Table("Perfil");
    }

}

PersonaLogin.cs

 public class PessoaLogin:Pessoa
{

    public virtual string Senha { get; set; }

    public virtual Perfil Perfil { get; set; }

}

PersonaLoginMap.cs

   public PessoaLoginMap()
    {

        KeyColumn("IdPessoa");

        Map(x => x.Senha)
            .Not.Nullable()
            .Length(MapLength.TextoMini);

        References(x => x.Perfil)
            .Columns("id_Perfil");

        Table("PessoaLogin");


    }


}

When I go to webservice

If PersonLogin.Profile for Null appears normally example

NowifIaddaProfilewhenrequestingthePersonaLogingivemethefollowingmessage

WebAPiConfig.cs

varjson=config.Formatters.JsonFormatter;json.SerializerSettings.PreserveReferencesHandling=Newtonsoft.Json.PreserveReferencesHandling.Objects;config.Formatters.Remove(config.Formatters.XmlFormatter);json.SerializerSettings.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore;

PersonLoginController.cs

publicclassPessoaLoginController:ApiController{privateIPessoaLoginRepository_repository44;publicPessoaLoginController(){_repository44=newPessoaLoginRepository();}[HttpGet]publicHttpResponseMessageGetAll(){//varlist=_repository44.GetAll11();varlists=_repository44.GetAll();returnRequest.CreateResponse(HttpStatusCode.OK,lists);}[HttpGet]publicHttpResponseMessageGetById(intid){varacesso=_repository44.Get(id);if(acesso==null){returnRequest.CreateResponse(HttpStatusCode.NotFound);}returnRequest.CreateResponse(HttpStatusCode.OK,acesso);}[HttpGet]publicHttpResponseMessageLogin(stringnome,stringsenha){varobj=_repository44.ValidarLogin(nome,senha);if(obj==null){returnRequest.CreateResponse(HttpStatusCode.NotFound);}returnRequest.CreateResponse(HttpStatusCode.OK,obj);}[HttpPost]publicHttpResponseMessageIncluir([FromBody]PessoaLoginpessoalogin){pessoalogin=_repository44.Add(pessoalogin);if(pessoalogin!=null){returnRequest.CreateResponse(HttpStatusCode.Created,pessoalogin);}returnRequest.CreateResponse(HttpStatusCode.NotModified);}}

EDIT

GavetheError:

    
asked by anonymous 18.12.2014 / 18:21

1 answer

1

You are serializing a proxy, according to this code in comment. There is nothing wrong with how the serializer solves the problem. The reduction of serialized properties is what is the focus of the question:

[HttpGet] 
public HttpResponseMessage GetAll() 
{ 
    var lists = _repository44.GetAll(); 
    return Request.CreateResponse(HttpStatusCode.OK, lists);
}

The solution is to implement a contract resolver that serializes the object using the class definition, not the proxy:

public class NHibernateContractResolver : DefaultContractResolver {
    protected override List<MemberInfo> GetSerializableMembers(Type objectType) {
        if (typeof(INHibernateProxy).IsAssignableFrom(objectType)) {
            return base.GetSerializableMembers(objectType.BaseType);
        } else {
            return base.GetSerializableMembers(objectType);
        }
    }
}

When configuring the serializer, pass it the contract resolver (last line):

var json = config.Formatters.JsonFormatter; 
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; 
config.Formatters.Remove(config.Formatters.XmlFormatter); 
json.SerializerSettings.ReferenceLoopHandling =  Newtonsoft.Json.ReferenceLoopHandling.Ignore;
json.SerializerSettings.ContractResolver = new NHibernateContractResolver();
    
18.12.2014 / 19:26