0
I’m using Nhibernate with Webapi have 2 entities Profile & Personal Personal Login may have N Profile and Profile 1 Person Login. However, when I go to the Personal webservicelogin the Profile reference only works if it is NULL if I add a Profile, whenever it is Personal Login 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; }
}
Perfilmap.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");
}
}
Personal login.Cs
public class PessoaLogin:Pessoa
{
public virtual string Senha { get; set; }
public virtual Perfil Perfil { get; set; }
}
Personal loginmap.Cs
public PessoaLoginMap()
{
KeyColumn("IdPessoa");
Map(x => x.Senha)
.Not.Nullable()
.Length(MapLength.TextoMini);
References(x => x.Perfil)
.Columns("id_Perfil");
Table("PessoaLogin");
}
}
When will I consult the webservice
if Personlog in.Profile for Null appears normally example
Now if I Add a Profile when ordering People give me the following message.
Webapiconfig.Cs
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;
Personal
public class PessoaLoginController : ApiController
{
private IPessoaLoginRepository _repository44;
public PessoaLoginController()
{
_repository44 = new PessoaLoginRepository();
}
[HttpGet]
public HttpResponseMessage GetAll()
{
//var list = _repository44.GetAll11();
var lists = _repository44.GetAll();
return Request.CreateResponse(HttpStatusCode.OK, lists);
}
[HttpGet]
public HttpResponseMessage GetById(int id)
{
var acesso = _repository44.Get(id);
if (acesso == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, acesso);
}
[HttpGet]
public HttpResponseMessage Login(string nome, string senha)
{
var obj = _repository44.ValidarLogin(nome, senha);
if (obj == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, obj);
}
[HttpPost]
public HttpResponseMessage Incluir([FromBody] PessoaLogin pessoalogin)
{
pessoalogin = _repository44.Add(pessoalogin);
if (pessoalogin != null)
{
return Request.CreateResponse(HttpStatusCode.Created, pessoalogin);
}
return Request.CreateResponse(HttpStatusCode.NotModified);
}
}
EDIT
Gave the Error:
There is nothing wrong with this behavior. What you would like to appear?
– Leonel Sanches da Silva
Hello Gypsy would like to get the reference ID. ---- [{ "id": "1", "Password":"323232", "Profile": 1, "Idpessoa":1, "Name":"JOEL SANTANA"}] ----
– Daniel Machado
You want me to serialize just these fields, right?
– Leonel Sanches da Silva
It only works if it is NULL if in the Bank I set the People Idprofile login as NULL it appears to me correct. otherwise of this JSON bug I cannot restore this Person login.Profile
– Daniel Machado
That this Profile Field without giving that error.
– Daniel Machado
What are you using to return JSON from the Controller? You can edit your question by placing this part?
– Leonel Sanches da Silva
WebApiConfig.css / 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;
– Daniel Machado
Personlogincontroller.css / [Httpget] public Httpresponsemessage Getall() { //var list = _repository44.Getall11(); var lists = _repository44.Getall(); Return Request.Createresponse(Httpstatuscode.OK, lists); }
– Daniel Machado
Repository.css / public Ilist<T> Getall() { Return _Connection.session.Createcriteria(typeof(T)). List<T>(); }
– Daniel Machado
I was told that it can be infinite loop when I require this Getall Method, so I followed some tutorials to insert that code block in Webapiconfig.css only that even this zicado the reference
– Daniel Machado
Please enter the codes in your question, and not as comments. I will reply.
– Leonel Sanches da Silva
Posted........
– Daniel Machado