2017-06-04 165 views
1

我嘗試將我的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的,我不希望從我的請求中獲得。我怎樣才能改變這種行爲?我只需要這個表coulmn值,而不是其他表。

請求結果:

{ 
    "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" 
} 

回答

1

我假設你有你的EntityFramework的啓用的DbContext延遲加載。當序列化發生時,每個屬性都被訪問並且EF會爲你加載它。

這種情況的好解決方案是刪除ICollection屬性中的virtual關鍵字。這是選擇性禁用延遲加載。

您還可以通過這樣

public class YourContext : DbContext 
{ 
    public YourContext() 
    { 
     // Default behaviour is true 
     this.Configuration.LazyLoadingEnabled = false; 
    } 
} 
禁用延遲加載所有在一起