2016-10-22 21 views
2

我有這樣的:如何使1:用不同的查詢(LINQ語法)模型之間的多對多關係

型號:

{ 
    [Table("Requests")] 
    public partial class RequestsModel 
    { 
     public RequestsModel() 
     { 
      this.CountView = new HashSet<RequestCountViewModels>(); 
     } 
     [Key] 
     public int Id { get; set; } 
     public int? Sender { get; set; } 
     public int? Type { get; set; } 
     public string Subject { get; set; } 
     public string Text { get; set; } 
     public int? Adtype { get; set; } 
     public int? Status { get; set; } 
     [Column(TypeName = "date")] 
     public DateTime? SDate { get; set; } 
     [Column(TypeName = "date")] 
     public DateTime? EDate { get; set; } 
     public DateTime? RDate { get; set; } 
     public DateTime? PayDate { get; set; } 
     public DateTime? RespDate { get; set; } 
     public long? Counter { get; set; } 
     public string Tags { get; set; } 
     public string Maps { get; set; } 
     public int? AccBy { get; set; } 

     public virtual ICollection<RequestCountViewModels> CountView { get; set; } 
    } 
    [Table("Counter")] 
    public partial class RequestCountViewModels 
    { 
     [Key] 
     public long Id { get; set; } 
     [ForeignKey("ParentMdl")] 
     public int? ReqId { get; set; } 
     public string IP { get; set; } 

     public virtual RequestsModel ParentMdl { get; set; } 

     public DateTime? Time { get; set; } 

    } 

} 

的HomeController:

public virtual ActionResult Advs(string id) 
    { 
     var model = _requestService.GetAdvertise(Convert.ToInt32(id)); 
     return View(model); 

    } 

RequestsService.cs

public RequestsModel GetAdvertise(int AdID) 
     { 
      return 
       _ctx.Requests 
       .AsNoTracking() 
       .FirstOrDefault(a => a.Id == AdID && a.Type == 1); 
     } 

RequestsConfig.cs

HasMany(a => a.CountView) 
       .WithRequired(a => a.ParentMdl) 
       .HasForeignKey(a => a.ReqId); 

這些[R我的代碼,現在我要讓RequestCountViewModel關係RequestsModel和之間使從刪除數據庫 檢索相關表中的數據將get請求表,但需要檢索每行的viewcount表並填入ICOLLECTION 幫我!對不起弱英語

+0

抱歉,**從遠程分貝** –

+0

'返回_ctx.Requests.Include(p值=> p.CountView).AsNoTracking()。FirstOrDefault (a => a.Id == AdID && a.Type == 1);' –

+0

@CristianSzpisjak當我編寫.Include(p => p.CountView)時,有紅色下劃線的語法錯誤!說:**不能將lambda表達式轉換爲類型'字符串',因爲它不是委託類型**!我不知道該怎麼辦 ! –

回答

0

好了,你需要做這樣的事情:

public GetData() 
{ 
    var data = _ctx.Requests 
     .Include(p => p.CountView) 
     .AsNoTracking() 
     .Select(a => a.CountView) 
     .ToList(); 

    return data; 
} 
+0

還沒有工作。我認爲在兩個表格之間建立關係存在一個問題。但我不知道是什麼,調試器不溫暖我!它無法爲RequestViewCountModel創建外鍵。幫我 ! –

+0

但上面的函數返回的東西? –

+0

它返回RequestModel,但不是ViewCountModel!返回22倍(ViewCount.Count),但集合是空的...我認爲foreignkey沒有創建或類似的東西, –

0

我發現我自己的問題的解決方案 RequestCountViewModels應該是這樣的:

[Table("Counter")] 
    public partial class RequestCountViewModels 
    { 
     public long Id { get; set; } 
     public string IP { get; set; } 
     [ForeignKey("ReqId")] 
     public virtual RequestsModel ParentMdl { get; set; } 
     public int ReqId { get; set; } 
     public DateTime Time { get; set; } 
    } 

RequestService.cs

public virtual RequestsModel GetAdvertises(int AdID) 
     { 
      return _requests 
       .AsNoTracking() 
       .Include(v => v.ViewCounts) 
       .Cacheable() 
       .FirstOrDefault(a => a.Id == AdID && a.Type == 1); 
     } 

並在視圖中,for loop語句應該被用來代替foreach