Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在EF MVC中应用左内联接_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc 在EF MVC中应用左内联接

Asp.net mvc 在EF MVC中应用左内联接,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我有以下SQL语法: 如何将该语法应用于MVC中的EF sytax?我目前在EF中的语法是: var recordManagementAPs = db.RecordManagementAPs.Include(r => r.MasterAP).Include(r => r.MasterLocation).Include(r => r.MasterRack); return View(recordManagementAPs.ToList()); 有什么想法吗? 非常感谢。 谢谢。

我有以下SQL语法:

如何将该语法应用于MVC中的EF sytax?我目前在EF中的语法是:

var recordManagementAPs = db.RecordManagementAPs.Include(r => r.MasterAP).Include(r => r.MasterLocation).Include(r => r.MasterRack);
return View(recordManagementAPs.ToList());
有什么想法吗? 非常感谢。 谢谢。

投影

为视图的数据声明视图模型。而不是传递实体

[Serializable]
public class RecordDetailViewModel
{
    public int MasterAPId { get; set; }
    public string Year { get; set; }
    public string Month { get; set; }
    public string DocumentNumber { get; set; }
    public string BoxNumber { get; set; }
    public string LocationShortName { get; set; }
    public string RackShortName { get; set; }
}
将实体查询投影到视图模型中并返回:

var viewModels = db.MasterAPs
    .Where(x => x.Month == month) // "2"
    .Select(x => new RecordDetailViewModel
    {
        MasterAPId = x.MasterAPId,
        Year = x.Year,
        Month = x.Month,
        DocumentNumber = x.DocumentNumber,
        BoxNumber = x.RecordManagementAP.BoxNumber,
        LocationShortName = x.MasterLocation.LocationShortName,
        RackShortName = x.MasterRack.RackShortName
     }).ToList();
假设您的实体关系已映射


当您返回整个实体并希望预取相关实体时,会使用Include关键字。如果访问这些实体,则会包含它们,而不是延迟加载它们。通常,如果要执行更新之类的操作,即接受视图模型或一组字段以从视图更新、加载实体并在保存实体之前应用更新,则仅应检索实体和相关实体。不要将实体传递给视图。这会暴露更多有关域的信息,需要更多内存,并且在序列化关系时可能会导致错误或性能缺陷。从视图接收实体更糟糕,因为它会使您的系统容易受到意外篡改、潜在的附加引用错误以及使用过时更改覆盖数据的攻击。

您可以通过以下方式编写查询:

var query = (from a in Context.MasterAP
            join b in Context.RecordManagementAP on new { a.MasterAPID } equals new { b.MasterAPID }
            into b1
            from b in b1.DefaultIfEmpty()
            join c in Context.MasterLocation on new { b.LocationID } equals new { c.LocationID }
            into c1
            from c in c1.DefaultIfEmpty()
            join d in Context.MasterRack on new { b.RackID } equals new { d.RackID }
            into d1
            from d in d1.DefaultIfEmpty()
            where a.Month == 2
            select new //Write YourModelName Here After new kwyword
            {
                Year = a.Year,
                Month = a.Month,
                DocumentNumber = a.DocumentNumber,
                BoxNumber = b.BoxNumber,
                LocationShortName = c.LocationShortName, 
                RackShortName = d.RackShortName
            });
var query = (from a in Context.MasterAP
            join b in Context.RecordManagementAP on new { a.MasterAPID } equals new { b.MasterAPID }
            into b1
            from b in b1.DefaultIfEmpty()
            join c in Context.MasterLocation on new { b.LocationID } equals new { c.LocationID }
            into c1
            from c in c1.DefaultIfEmpty()
            join d in Context.MasterRack on new { b.RackID } equals new { d.RackID }
            into d1
            from d in d1.DefaultIfEmpty()
            where a.Month == 2
            select new //Write YourModelName Here After new kwyword
            {
                Year = a.Year,
                Month = a.Month,
                DocumentNumber = a.DocumentNumber,
                BoxNumber = b.BoxNumber,
                LocationShortName = c.LocationShortName, 
                RackShortName = d.RackShortName
            });