c# - 如何制作 1 :many relationship between models with different query ( linq syntax )

标签 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; }

    }

}

家庭 Controller :

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

这些是我的代码,现在我想在 RequestsModelRequestCountViewModel 之间建立关系,以便从删除数据库中检索相关的表数据 它会得到请求表,但需要为每一行检索 viewcount 表并填写到 ICOLLECTION 帮我!抱歉英语不好

最佳答案

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

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

    return data;
}

关于c# - 如何制作 1 :many relationship between models with different query ( linq syntax ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40192285/

相关文章:

c# - 如何避免 MySql/net 连接器中的 "There is already an open DataReader associated with this Connection which must be closed first."?

c# - 更改 foreach 语句中的 Linq 对象?

c# - Entity Framework 在三个表中提取空值

c# - log4net什么时候登录?

c# - 无法加载文件或程序集 'System.Web.Mvc' 或其依赖项之一。 2个项目。一次只有一个会工作

c# - Razor View 中的 Mvc5 VS2013 错误

asp.net - HttpClient 可以有多个 HTTP 消息处理程序吗?

javascript - 将值传递给 Controller

asp.net-mvc - 提交时asp.net mvc Forms Collection

c# - LINQ Lambda 连接与计数