我尝试将我的 Java EE 项目重写为 C# Rest API 项目。 对于数据库映射,我使用实体框架,并且在从表中获取记录时遇到一些问题。 首先,我有一个被其他表引用的表:
CREATE TABLE Bibliogr (
Bknumber INT PRIMARY KEY NOT NULL,
Authors VARCHAR(500),
Source VARCHAR(500),
Title VARCHAR(1000)
);
映射的类如下:
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; }
}
有很多我不想从我的请求中获得的HashSet。我怎样才能改变这种行为?我只需要这个表的值,而不需要其他表。
请求结果:
{
"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"
}
我假设您在 EntityFramework 的 DbContext 中启用了延迟加载。当序列化发生时,每个属性都会被访问,并且 EF 会为您加载它。
这种情况的好解决方案是删除
virtual
属性上的 ICollection
关键字。这是选择性禁用延迟加载。
您还可以通过这样做来完全禁用延迟加载
public class YourContext : DbContext
{
public YourContext()
{
// Default behaviour is true
this.Configuration.LazyLoadingEnabled = false;
}
}