如何使用不同查询的模型之间建立1:多关系(linq语法)

时间:2016-10-22 12:39:57

标签: c# asp.net-mvc linq ef-fluent-api

我有这个:

型号:

{
   [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);

这些是我的代码,现在我想在 RequestsModel RequestCountViewModel 之间建立关系,以便从删除db中检索相关的表数据 它将获取请求表,但需要检索每行的viewcount表并填入ICOLLECTION 帮我!抱歉英语不好

2 个答案:

答案 0 :(得分:0)

好的,所以你需要做这样的事情:

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

    return data;
}

答案 1 :(得分: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