Tengo la siguiente clase Company , la cual posee un atributo de tipo List , en el momento que hago el GET a el EndPoint
http://localhost:60391/api/DataCompany?NameCompany=Hp
me devuelve el error:Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'Company' with type 'CoreTest.Models.Context.Company'. Path 'Subsidiary[0]'.'
¿Como puedo solucionar esto?
Clase Company
public class Company
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 1, TypeName = "INT")]
public int Id { get; set; }
[Required]
[Column(Order = 2, TypeName = "VARCHAR(50)")]
public string NameCompany { get; set; }
[Required]
[Column(Order = 3, TypeName = "VARCHAR(50)")]
public string BusinessBranch { get; set; }
[Required]
[Column(Order = 4, TypeName = "VARCHAR(50)")]
public string Email { get; set; }
[Column(Order = 5, TypeName = "VARCHAR(15)")]
public string Phone { get; set; }
[Required]
[Column(Order = 6, TypeName = "FLOAT")]
public float AnnualGross { get; set; }
[Column(Order = 7, TypeName = "DATETIME")]
public DateTime CreateDate { get; set; }
[Column(Order = 8, TypeName = "VARCHAR(10)")]
public string TypeCompany { get; set; }
public List<Subsidiary> Subsidiary { get; set; }
}
Clase Subsidiary:
public class Subsidiary
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 1, TypeName = "INT")]
public int Id { get; set; }
[Column(Order = 2, TypeName = "INT")]
[ForeignKey("CompanyId")]
public Company Company { get; set; }
[Column(Order = 3, TypeName = "INT")]
public int IdCompany { get; set; }
[Required]
[Column(Order = 4, TypeName = "VARCHAR(50)")]
public string NameSubsidiary { get; set; }
[Required]
[Column(Order = 5, TypeName = "VARCHAR(50)")]
public string Email { get; set; }
[Column(Order = 6, TypeName = "VARCHAR(15)")]
public string Phone { get; set; }
[Required]
[Column(Order = 7, TypeName = "FLOAT")]
public float AnnualGross { get; set; }
[Column(Order = 8, TypeName = "DATETIME")]
public DateTime CreateDate { get; set; }
[Column(Order = 9, TypeName = "VARCHAR(10)")]
public string TypeSubsidiary { get; set; }
}
Metodo :
[HttpGet]
[ActionName("GetClient")]
public string GetClient (string NameCompany)
{
string response = EngineValue.NoData;
if (NameCompany == string.Empty || NameCompany == null)
{
response = HttpStatusCode.BadRequest.ToString(); ;
return response;
}
EngineDb Metodo = new EngineDb();
Company client = new Company();
client = Metodo.GetClient(NameCompany, context);
if (client == null)
return response;
return response = JsonConvert.SerializeObject(client);
}