使用Entity Framework和Linq对不同的表进行相同的选择 [英] Make the same select to different tables using Entity Framework and Linq

查看:108
本文介绍了使用Entity Framework和Linq对不同的表进行相同的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好!我将解释我不想做的事情:

Ok! I'm going to explain what I don't want to do:

我正在制作报告,该报告由多个表格所符合:

I'm making a report, this report is conformed by multiples tables:

这是我数据库的实体关系图。我的前端有这样的Json:

this is the entity relation diagram of my data base. I have a Json like this in my front-end:

var json = {
  DatesReport: [],
  Maquinas: [],
  Faltantes: [],
   ParosLinea: [],
   Reinsertos:[],
   Auditado: [],
  Pendientes: [],
  TiempoExtra: [],
  Scrap: [],
  Criticos: [],
  Accidentes: []
};

我已经有逻辑填写这个Json,然后将其发送到我的后端。

I already have the logic to fill this Json and then send this one to my back-end.

这是我接收json的方法从前端到后端,然后以这种方式插入所有值

This is my method that receives the json of from front-end to back-end then insert all values at this way

这是我的班级报告人:

this is my class reporte:

所以我的问题是我找不到找到类似于已经插入所有不同表的所有数据的方法的方法

So my problem is that I don't find the way to make something similar to what I already have to insert all data of all different tables

我正在寻找一种方法来在后端的所有不同表中进行选择,形成json并将其发送到前端

I'm looking for a way to make a select at all different tables in my back-end, form my json and sent it to my front-end

我可以这样:

I Could made with this way:

但是我正在寻找实现它的最佳方法,我正在尝试使代码简短且可维护

But I'm looking for the best way to make it, I'm trying to make my code short, and Maintainable

此外,这是我的dbcontext

in addition this is my dbcontext

推荐答案

看到数据库模型,您应该有一个包含所有集合的类 DatesReport 。因此,而不是像这样的Json对象……

Seeing the database model you should have a class DatesReport that contains all collections. So instead of a Json object like...


{
  DatesReport: [],
  Maquinas: [],
  Faltantes: [],
  ParosLinea: [],
  Reinsertos:[],
  Auditado: [],
  Pendientes: [],
  TiempoExtra: [],
  Scrap: [],
  Criticos: [],
  Accidentes: []
}


...应该有一个看起来像...

...there should be a class looking like...

class DatesReport
{
    public int IdReport { get; set; }
    ... more properties

    public ICollection<Maquina> Maquinas { get; set; }
    public ICollection<Faltante> Faltantes { get; set; }
    ... etc.

}

public DbSet<DatesReport> Reports { get; set; }

现在,您只需一个条件即可获得所需的数据:

Now you can get the data you want by only one Where condition:

var report = context.Reports
    .Include(r => r.Maquinas)
    .Include(r => r.Faltantes)
    ... etc
    .Single(r => r.Id == id)

序列化的报告看起来与您现在拥有的Json不同,但是UI代码应该可以处理它。同样,它应返回包含这些集合的 DatesReport 对象。如果是这样,添加新报告将变得非常简单:

The serialized report will look different than the Json you have now, but the UI code should be able to handle that. Likewise, it should return a DatesReport object containing these collections. If it does, adding a new report gets very simple:

context.Reports.Add(report);
context.SaveChanges();

其中 report 是输入的报告对象操作方法 MetodoRecibe 而不是 datos 对象。它将一次性添加报告和所有包含的集合。

Where report is the report object that enters the action method MetodoRecibe instead of the datos object. It will add the report and all contained collections in one go.

这篇关于使用Entity Framework和Linq对不同的表进行相同的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆