我正在尝试获取对象的所有属性。我不知道如何使用我正在使用的关系类型来做到这一点。我将返回db中的所有Jobs。 Jobs与CustomerEmployee类有一对多的关系。我需要通过Job列表访问CustomerEmployee类中的所有属性。我成功获得了First& CustomerEmployee的姓氏,但我看不到如何使用这种方式访问其他属性。所以我不知道我是否需要改变设置关系的方式?这引出了我的下一个问题,当我创建一个新的Job Post时,外键没有插入右栏。 db有两个不同的列。例如,“JobTESPMId”& “JobTESPMId_EmployeeId”。我需要在“JobTESPMId_EmployeeId”中插入Id,但它正在插入另一个中。设置它时我有点失落,所以我想明白为什么会发生这种情况。
public class Job
{
public Int64? JobId { get; set; }
public int? JobNumber { get; set; }
public string JobName { get; set; }
public string JobDescription { get; set; }
public int? GeoAreaId { get; set; }
public virtual JobMisc.GeoArea GeoArea { get; set; }
public int? JobClassId { get; set; }
public virtual JobMisc.JobClass JobClass { get; set; }
public int? JobTypeId { get; set; }
public virtual JobMisc.JobType JobType { get; set; }
public Int64? CustomerId { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<ChangeOrder> ChangeOrders { get; set; }
public virtual ICollection<PurchaseOrder> PurchaseOrders { get; set; }
public virtual ICollection<JobItem> JobItems { get; set; }
public int? CustomerEmployeePMId { get; set; }
public virtual CustomerEmployee CustomerEmployeePM { get; set; }
public int? CustomerEmployeeAdminId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAdmin { get; set; }
public int? CustomerEmployeeAccountantId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAccountant { get; set; }
public int? CustomerEmployeeSuperintendentId { get; set; }
public virtual CustomerEmployee CustomerEmployeeSuperintendent { get; set; }
public int? JobTESPMId { get; set; }
public virtual Employee JobTESPM { get; set; }
public int? JobTESSuperintendentId { get; set; }
public virtual Employee JobTESSuperintendent { get; set; }
}
CustomerEmployee
public class CustomerEmployee
{
[Key]
public int CustomerEmployeeId { get; set; }
public string CustomerEmployeeFirstName { get; set; }
public string CustomerEmployeeLastName { get; set; }
public string CustomerEmployeeEmail { get; set; }
public Int64? CustomerEmployeePhoneNumber { get; set; }
public Int64? CustomerEmployeeCellNumber { get; set; }
public Int64? CustomerEmployeeFaxNumber { get; set; }
public bool? CustomerEmployeeIsHidden { get; set; }
public string CustomerEmployeeRole { get; set; }
public Int64? CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
视图模型/结果
public class JobViewModel
{
public Int64? JobId { get; set; }
public int? JobNumber { get; set; }
public string JobName { get; set; }
public decimal? JobOriginalContract { get; set; }
public DateTime? JobContractDate { get; set; }
public decimal? JobTotalCO { get; set; }
public decimal? JobRevisedContract { get; set; }
public decimal? JobOriginalBudget { get; set; }
public string JobStatus { get; set; }
public bool? JobTaxExempt { get; set; }
public bool? JobCertPayroll { get; set; }
public decimal? JobCost { get; set; }
public decimal? JobRemainingBudget { get; set; }
public decimal? JobProfit { get; set; }
public decimal? JobPercentage { get; set; }
public decimal? JobTotalBilled { get; set; }
public decimal? JobBalanceToBill { get; set; }
public decimal? JobPaidToDate { get; set; }
public decimal? JobBalanceDue { get; set; }
public bool? JobIsHidden { get; set; }
public string Customer { get; set; }
public string CustomerEmployeePM { get; set; }
public string JobTESPM { get; set; }
public string JobTESSuperintendent { get; set; }
public IEnumerable<ChangeOrder> ChangeOrders { get; set; }
public IEnumerable<PurchaseOrder> PurchaseOrders { get; set; }
}
public class JobResult
{
public Int64? JobId { get; set; }
public int? JobNumber { get; set; }
public string JobName { get; set; }
public decimal? JobOriginalContract { get; set; }
public DateTime? JobContractDate { get; set; }
public decimal? JobTotalCO { get; set; }
public decimal? JobRevisedContract { get; set; }
public decimal? JobOriginalBudget { get; set; }
public string JobStatus { get; set; }
public bool? JobTaxExempt { get; set; }
public bool? JobCertPayroll { get; set; }
public decimal? JobCost { get; set; }
public decimal? JobRemainingBudget { get; set; }
public decimal? JobProfit { get; set; }
public decimal? JobPercentage { get; set; }
public decimal? JobTotalBilled { get; set; }
public decimal? JobBalanceToBill { get; set; }
public decimal? JobPaidToDate { get; set; }
public decimal? JobBalanceDue { get; set; }
public bool? JobIsHidden { get; set; }
public string Customer { get; set; }
public string CustomerEmployeePM { get; set; }
public string JobTESPM { get; set; }
public string JobTESSuperintendent { get; set; }
public IEnumerable<ChangeOrder> ChangeOrders { get; set; }
public IEnumerable<PurchaseOrder> PurchaseOrders { get; set; }
}
apiController
// GET api/<controller>
public IEnumerable<JobResult> Get()
{
using (var context = new ApplicationDbContext())
{
return context.Jobs
.Include(x => x.Customer)
.Include(x => x.ChangeOrders)
.Include(x => x.PurchaseOrders)
.Include(x => x.CustomerEmployeePM)
.Include(x => x.JobTESPM)
.Include(x => x.JobTESSuperintendent)
.ToResults();
}
}
JSON
0: {$id: "1", JobId: 2, JobNumber: 3244, JobName: "Job Alpha", JobOriginalContract: 34343443,…}
$id: "1"
ChangeOrders: [,…]
Customer: "Twin Peaks"
CustomerEmployeePM: "Kelly Young"
JobId: 2
JobName: "Job Alpha"
JobNumber: 3244
JobTESPM: "Laura Mince"
JobTESSuperintendent: "Scott Willis"
JobTaxExempt: true
JobTotalBilled: null
PurchaseOrders: []
1: {$id: "3", JobId: 9, JobNumber: 342, JobName: "sad", JobOriginalContract: 323232,…}
答案 0 :(得分:0)
return context.Jobs
.Include(x => x.Customer)
.Include(x => x.ChangeOrders)
.Include(x => x.PurchaseOrders)
.Include(x => x.CustomerEmployeePM)
.Include(x => x.JobTESPM)
.Include(x => x.JobTESSuperintendent)
.ToList();
是你需要的
答案 1 :(得分:0)
可能是一个快速回答,如果您的JobResult类有一个允许您从Job映射到JobResult的构造函数,那么它就像
一样简单return context.Jobs
.Select(j => new JobResult(j));
或者如果你喜欢较少耦合的方式......
return context.Jobs
.Select(j => new JobResult(j.Customer, j.ChangeOrders, j.PurchaseOrders, etc.));
基本上,您正在处理从一种类型到另一种类型的映射。这种映射工作也是你解决第二个问题的方法。
编辑:我的EF能力有点模糊,但你可能需要一个空的构造函数,在这种情况下是这样的......我有点忘了linq查询中允许的内容但是我知道一个空的构造函数可以工作。
return context.Jobs
.Select(j => new JobResult() { j.Customer, j.ChangeOrders, j.PurchaseOrders, etc.} );
如果失败了,如果你知道你想要所有工作而没有分页/过滤链......那么......
return context.Jobs
.ToList()
.Select(j => new JobResult() { j.Customer, j.ChangeOrders, j.PurchaseOrders, etc.} );