I have the following relationships in SQL:
The 3 are related by the field factura
, the point is that I am entering an invoice number and I want it to bring me the data from the table first Materiales
and then the other 2 (I want to do it like this one by one because I don't know how to call everything on the whole).
Since I don't know how to call them together, I want to first bring this data (example) from my table Materiales
:
CODE TO BRING THE DATA
//TRAER CABEZA DE LA FACTURA
public List<Materiale> dataUnica(string fac)
{
var lista = new List<Materiale>();
using(var ctx=new ModelContext())
{
lista = ctx.Materiales.Where(x => x.factura == fac)
.ToList();
}
return lista;
}
CODE WHERE I PASS THE DATA TO THE AJAX BACK
[HttpGet]
public string getFacturaData(string ff)
{
return JsonConvert.SerializeObject(mT.dataUnica(ff));
}
In the JSON line I am having the following error:
Newtonsoft.Json.JsonSerializationException: 'Error getting value from 'Det_mate2' on 'System.Data.Entity.DynamicProxies.Materiale_5B9A5C2FF9BBF9788B0E565D9631BB85E36ED4F9804447E308EBA79DAB4A36A8'.' ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
TABLE CODEMATERIALE
public partial class Materiale
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Materiale()
{
Det_materiales = new HashSet<Det_materiales>();
Det_mate2 = new HashSet<Det_mate2>();
}
[Key]
[StringLength(50)]
public string factura { get; set; }
[Required]
[StringLength(50)]
public string Invoice { get; set; }
[Column(TypeName = "date")]
public DateTime fecha { get; set; }
[Required]
[StringLength(50)]
public string tipo_via { get; set; }
public double pesobruto { get; set; }
public double pesoneto { get; set; }
public int numerobultos { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Det_materiales> Det_materiales { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Det_mate2> Det_mate2 { get; set; }
}
I feel that it is pulling the data from the other tables using the invoice number, that's why the error (from my ignorance) how do I avoid this, to only pull the data from the table Materiales
?
Serialization Lazy Loanding
That happens when you use lazy loading and add virtual to your properties. To fix this you have to use an explicit load method with the
include
.First disable lazy loading:
And you remove the virtual from these properties:
So to charge your bills
Then you have to specify which property you are going to load in the method,
include
this will globally solve the serialization problem and load the relations only when you need it, it will help you in performance.