2017-09-29 26 views
0

我有一个带有另一个表的外键的模型。在这种情况下,减免对象涉及到办公室对象MVC Core:显示基于主键的另一个属性

public class Waiver 
    { 
     public int WaiverID { get; set; } 
     [Required(ErrorMessage = "Please select an Office")] 
     [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")] 
     public int OfficeID { get; set; } 
     //...other properties 
} 

我有一个由OfficeID过滤搜索结果:

@model IEnumerable<Waiver> 

@foreach (var item in Model) 
    { 
     <tr> 
      <td class="text-right">@item.WaiverID</td> 
      <td class="text-right">@item.OfficeID></td> 
@* other display columns *@ 

IEnumerable<Waiver> waiverList = repository.Waivers 
       .Where(wl => waiverNum == 0 || wl.WaiverID == waiverNum) 
       .Where(wl => officeId == 0 || wl.OfficeID == officeId) 
//...other search criteria 

这是通过显示在我看来,

尽管传递给View的Waiver对象具有OfficeID属性,但我想在Office模型中显示Name属性,因为它对用户更有用。我怎样才能做到这一点?

我的EF库被设置为:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 

namespace Waiver.Models 

//This class implements the IWaiverRepository and gets its data using Entity Framework Core 
{ 
    public class EFWaiverRepository : IWaiverRepository 
    { 


     private ApplicationDbContext context; 

     public EFWaiverRepository(ApplicationDbContext ctx) 
     { 
      context = ctx; 
     } 


     public IEnumerable<Waiver> Waivers => context.Waivers; 
     public IEnumerable<Office> Offices => context.Offices; 

     //*****Waiver Methods***** 
     public void SaveWaiver(Waiver waiver) 
     { 
      //if the waiver number is 0, create a new waiver 
      if (waiver.WaiverID == 0) 
      { 
       context.Waivers.Add(waiver); 
      } 
      //if there wavier number exists, save the changes to the database 
      else { 
       Waiver dbEntry = context.Waivers.FirstOrDefault(w => w.WaiverID == waiver.WaiverID); 
       if (dbEntry != null) 
       { 
        dbEntry.Requestor = waiver.Requestor; 
        dbEntry.RequestorEmail = waiver.RequestorEmail; 
        dbEntry.OfficeID = waiver.OfficeID; 
        dbEntry.RequestDate = waiver.RequestDate; 
        dbEntry.System = waiver.System; 
        dbEntry.Source = waiver.Source; 
        dbEntry.Requirement = waiver.Requirement; 
        dbEntry.MitigationPlan = waiver.MitigationPlan; 
        dbEntry.FinalAssessment = waiver.FinalAssessment; 
        dbEntry.Status = waiver.Status; 
        //get the signatures 
        dbEntry.PmSignature = waiver.PmSignature; 
        dbEntry.PmSignDate = waiver.PmSignDate; 
       } 
      } 
      context.SaveChanges(); 
     } 

     public Waiver DeleteWaiver(int waiverID) 
     { 
      Waiver dbEntry = context.Waivers 
       .FirstOrDefault(w => w.WaiverID == waiverID); 
      if (dbEntry != null) 
      { 
       context.Waivers.Remove(dbEntry); 
       context.SaveChanges(); 
      } 
      return dbEntry; 
     } 

     //*****Office Methods***** 
     public void SaveOffice(Office office) 
     { 
      //if the ID=0, its a new entry. Add to db 
      if (office.OfficeID == 0) 
      { 
       context.Offices.Add(office); 
      } else 
      { 
       Office dbEntry = context.Offices.FirstOrDefault(o => o.OfficeID == office.OfficeID); 
       if (dbEntry != null) 
       { 
        dbEntry.Name = office.Name; 
        dbEntry.SiteID = office.SiteID; 
       } 
      } 
      context.SaveChanges(); 
     } 

     public Office DeleteOffice(int officeID) 
     { 
      Office dbEntry = context.Offices 
       .FirstOrDefault(o => o.OfficeID == officeID); 
      if (dbEntry != null) 
      { 
       context.Offices.Remove(dbEntry); 
       context.SaveChanges(); 
      } 
      return dbEntry; 
     } 

其中EFRepository实现:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 

namespace Waiver.Models 
{ 
    public interface IWaiverRepository 
    { 
     IEnumerable<Waiver> Waivers { get; } 
     IEnumerable<Office> Offices { get; } 
     IEnumerable<Site> Sites { get; } 

     void SaveWaiver(Waiver waiver); 
     Waiver DeleteWaiver(int waiverID); 

     void SaveOffice(Office office); 
     Office DeleteOffice(int officeID); 
    } 
} 
+0

你在使用实体框架核心吗?它是什么版本? – DSR

回答

1

对于EF Core,延迟加载还不可能,因此您需要包含相关实体。解决方案应该如下所示。

public class Waiver 
{ 
    public int WaiverID { get; set; } 
    [Required(ErrorMessage = "Please select an Office")] 
    [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")] 
    public int OfficeID { get; set; } 

    public virtual Office Office { set;get;} 
} 

repository.Waivers需要更改如下。

更新

IEnumerable<Waiver> waiverList = repository.Waivers.Where(wl => waiverNum == 0 || wl.WaiverID == waiverNum).Where(wl => officeId == 0 || wl.OfficeID == officeId) 

//...other搜索条件

@model IEnumerable<Waiver> 
<table> 
@foreach (var item in Model) 
{ 
    <tr> 
     <td> @item.WaiverID </td> 
     <td> @item.OfficeID </td> 
     <td> @item.Office.Name </td> 
    </tr> 
} 
</table> 

更新

变化public IEnumerable<Waiver> Waivers => context.Waivers;

public IEnumerable<Waiver> Waivers => context.Waivers.Include(o => o.Office); 

如果这不起作用,您需要检查您的db上下文类,并在模型绑定中正确设置WaiversOffice表之间的映射。

希望这会有所帮助。

+0

这是有道理的,但我在“包含”方法中出现错误,因为它不被识别。这是在更新的EF吗?我正在使用1.1.1,但在尝试更新到EFCore 2.0.0时出现错误 – coolhand

+0

不,这应该适用于您的版本,请查看此官方文章。 https://docs.microsoft.com/en-us/ef/core/querying/related-data。只需要这两个参考。使用微软。EntityFrameworkCore;和 using System.Linq; – DSR

+0

我认为您使用的是存储库模式,因此您需要在检索数据时更改存储库以支持包含功能。 – DSR

0

添加virtual导航性能上的豁免类Office类型。

public class Waiver 
{ 
    public int WaiverID { get; set; } 
    [Required(ErrorMessage = "Please select an Office")] 
    [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")] 
    public int OfficeID { get; set; } 

    public virtual Office Office { set;get;} 
} 

现在,在您看来,您可以访问每个豁免项目

@model IEnumerable<Waiver> 
<table> 
@foreach (var item in Model) 
{ 
    <tr> 
     <td> @item.WaiverID </td> 
     <td> @item.OfficeID </td> 
     <td> @item.Office.Name </td> 
    </tr> 
} 
</table> 

EF将加载您的豁免项目写字楼物业的写字楼物业。

相关问题