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: