Reputation: 663
I tries to rewrite my Java EE project to C# Rest API project. For database mapping I user Entity Framework, and have some problems with getting records from table. First of all I've got a table which is referred to by other tables:
CREATE TABLE Bibliogr (
Bknumber INT PRIMARY KEY NOT NULL,
Authors VARCHAR(500),
Source VARCHAR(500),
Title VARCHAR(1000)
);
The mapped class is following:
public partial class Bibliogr
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Bibliogr()
{
this.AcOpTabl = new HashSet<AcOpTabl>();
this.ConstSel = new HashSet<ConstSel>();
this.CuryTabl = new HashSet<CuryTabl>();
this.DecrTabl = new HashSet<DecrTabl>();
this.DensTabl = new HashSet<DensTabl>();
this.DielDiss = new HashSet<DielDiss>();
this.Dielectr = new HashSet<Dielectr>();
this.ElemTabl = new HashSet<ElemTabl>();
this.ElOpTabl = new HashSet<ElOpTabl>();
this.EquationTabl = new HashSet<EquationTabl>();
this.EsOpTabl = new HashSet<EsOpTabl>();
this.HardTabl = new HashSet<HardTabl>();
this.HeatExpn = new HashSet<HeatExpn>();
this.HeatTabl = new HashSet<HeatTabl>();
this.MechTabl = new HashSet<MechTabl>();
this.MnOpTabl = new HashSet<MnOpTabl>();
this.ModfTabl = new HashSet<ModfTabl>();
this.NlOpTabl = new HashSet<NlOpTabl>();
this.PlavTabl = new HashSet<PlavTabl>();
this.PzElTabl = new HashSet<PzElTabl>();
this.RefrcInd = new HashSet<RefrcInd>();
this.SuspTabl = new HashSet<SuspTabl>();
this.Wavepure = new HashSet<Wavepure>();
}
public int Bknumber { get; set; }
public string Authors { get; set; }
public string Source { get; set; }
public string Title { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AcOpTabl> AcOpTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ConstSel> ConstSel { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CuryTabl> CuryTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DecrTabl> DecrTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DensTabl> DensTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DielDiss> DielDiss { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Dielectr> Dielectr { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ElemTabl> ElemTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ElOpTabl> ElOpTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EquationTabl> EquationTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EsOpTabl> EsOpTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HardTabl> HardTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HeatExpn> HeatExpn { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<HeatTabl> HeatTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MechTabl> MechTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MnOpTabl> MnOpTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ModfTabl> ModfTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<NlOpTabl> NlOpTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PlavTabl> PlavTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PzElTabl> PzElTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<RefrcInd> RefrcInd { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SuspTabl> SuspTabl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Wavepure> Wavepure { get; set; }
}
There are a lot of HashSet which I don't want to get from my request. How can I change this behaviour? I need only this table coulmn values and not the other tables.
The request result:
{
"AcOpTabl":[ ],
"ConstSel":[ ],
"CuryTabl":[ ],
"DecrTabl":[ ],
"DensTabl":[ ],
"DielDiss":[ ],
"Dielectr":[ ],
"ElemTabl":[ ],
"ElOpTabl":[ ],
"EquationTabl":[ ],
"EsOpTabl":[ ],
"HardTabl":[ ],
"HeatExpn":[ ],
"HeatTabl":[ ],
"MechTabl":[ ],
"MnOpTabl":[ ],
"ModfTabl":[ ],
"NlOpTabl":[ ],
"PlavTabl":[ ],
"PzElTabl":[ ],
"RefrcInd":[ ],
"SuspTabl":[ ],
"Wavepure":[ ],
"Bknumber":4,
"Authors":"Niizeki N.,Yamada N.,Toyoda H.",
"Source":"Jap.J.Appl.Phys.,1967,v.6,N.3,p.318-327",
"Title":"Growth ridges, etched hillocks and crystal structure of lithium niobate"
}
Upvotes: 2
Views: 255
Reputation: 773
I am assuming you have lazy loading enabled in your EntityFramework's DbContext. When serialization happens, each property is accessed and EF loads it for you.
Good solution for this case is to remove the virtual
keywords on your ICollection
properties. This is selective disabling of lazy loading.
You can also disable lazy-loading all-together by doing this
public class YourContext : DbContext
{
public YourContext()
{
// Default behaviour is true
this.Configuration.LazyLoadingEnabled = false;
}
}
Upvotes: 1