我有这个 :

楷模:

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

    }

}


家用控制器:

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之间建立关系,以便从remove db中检索相关的表数据
它将获取请求表,但需要为每一行检索viewcount表并填写ICOLLECTION
帮我!对不起,英语不好

最佳答案

好的,因此您需要执行以下操作:

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

    return data;
}

关于c# - 如何在具有不同查询的模型之间建立1:许多关系(linq语法),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40192285/

10-09 18:27