在一对多关系中使用 Entity Framework Core 从 ASP.NET Core 中的三个相关表加载数据导致某些数据不显示
Loading data from three related tables in ASP.NET Core using Entity Framwork Core in one-to-many relationship makes some data not displayed
我有三个 table Customer
、Loan
和 LoanHistories
。 Customer
table与Loan
table是一对多关系,Loan
table与[=18有关=] 在一对多关系中也是如此。
以下是这些 table 的 C# 类:
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "ID NO")]
public int CustomerID { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Phone Number")]
public string PhoneNo { get; set; }
[Display(Name = "Nearest Primary School")]
public string NearestPrimarySchool { get; set; }
[Display(Name = "Photo")]
public string Photo { get; set; }
public ICollection<Loan> loans { get; set; }
// public IEnumerable<LoansHistories> loansHistories { get; set; }
// public ICollection<WorkForLiving> workForLivings { get; set; }
// public ICollection<CustomersCharacterBehavior> customersCharacterBehaviors { get; set; }
}
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
[NotMapped]
public decimal TotalRepaidIn { get; set; }
[NotMapped]
public decimal Balance { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Loan Balance")]
private decimal _LoanBalance;
public decimal LoanBalance
{
get { return LoanAmount * interestRate; }
set { _LoanBalance = value; }
}
[Display(Name = "Interest Rate")]
public decimal interestRate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Application Date")]
public DateTime ApplicationDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Disbursement Date")]
public DateTime DisbursmentDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Due Date")]
public DateTime DueDate { get; set; }
[Display(Name = "Defaulted")]
public bool Defaulted { get; set; }
[Display(Name = "Approved")]
public bool Approved { get; set; }
//Navigation property
public int CustomerID { get; set; }
public Customer customer { get; set; }
//public ICollection<LoanComments> loancomments { get; set; }
public ICollection<LoansHistories> loansHistories { get; set; }
}
public class LoansHistories
{
[Key]
public int HistID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Repaid Amount")]
public decimal RePaidIn { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Repayement Date")]
public DateTime RepayementDateDate { get; set; }
[Display(Name = "No of paying interest only")]
public int NoOfPayinyingIntrestOnly { get; set; }
// Navigation properties
public int LoanID { get; set; }
public Loan loan { get; set; }
// Navigation property
// public int CustomerID { get; set; }
// public Customer customer { get; set; }
}
这是从 Loans
和 LoansHistories
加载数据的 LoansController
tables:
[HttpGet]
public async Task<ActionResult<IEnumerable<Loan>>> Getloans()
{
var data = await _context.loans
.Include(lh => lh.loansHistories)
.Select(l => new Loan()
{
TotalRepaidIn = l.loansHistories.Select(lh => lh.RePaidIn).Sum(),
Balance = l.loansHistories.Select(lh => lh.RePaidIn).Sum()-l.LoanAmount,
loansHistories = l.loansHistories,
ApplicationDate = l.ApplicationDate,
Defaulted = l.Defaulted,
DisbursmentDate = l.DisbursmentDate,
DueDate = l.DueDate,
LoanID = l.LoanID,
Approved = l.Approved,
interestRate = l.interestRate,
LoanAmount = l.LoanAmount
}).ToListAsync();
return data;
}
这是JSON格式的数据:
[
{
"loanID":1,
"loanAmount":1000.0000,
"totalRepaidIn":202700.0000,
"balance":201700.0000,
"loanBalance":15000.000000,
"interestRate":15.00,
"applicationDate":"2022-03-28T00:00:00",
"disbursmentDate":"2022-03-28T00:00:00",
"dueDate":"2022-04-28T00:00:00",
"defaulted":false,
"approved":true,
"customerID":0,
"customer":null,
"loansHistories":[
{
"histID":1,
"rePaidIn":500.0000,
"repayementDateDate":"2022-03-28T00:00:00",
"noOfPayinyingIntrestOnly":1,
"loanID":1,
"loan":null
}
]
}
]
我想从客户控制器加载数据,但我发现实体 TotalRepaidIn 为零,我怎样才能使它 return 一个值在贷款控制器中有其 returning?没有实体 totalRepaidIn":0.0
客户控制员:
// GET: api/CustomersApi
[HttpGet]
public async Task<ActionResult<IEnumerable<Customer>>> Getcustomers()
{
return await _context.customers.Include(l=>l.loans).ThenInclude(h=>h.loansHistories).ToListAsync();
}
return以下Json数据:
[
{
"customerID":30290122,
"firstName":"Isaac",
"lastName":"Kiplagat",
"phoneNo":"0724797768",
"nearestPrimarySchool":"Mokwo",
"photo":"photo",
"loans":[
{
"loanID":1,
"loanAmount":1000.0000,
"totalRepaidIn":0.0,
"balance":0.0,
"loanBalance":15000.000000,
"interestRate":15.00,
"applicationDate":"2022-03-28T00:00:00",
"disbursmentDate":"2022-03-28T00:00:00",
"dueDate":"2022-04-28T00:00:00",
"defaulted":false,
"approved":true,
"customerID":30290122,
"loansHistories":[
{
"histID":1,
"rePaidIn":500.0000,
"repayementDateDate":"2022-03-28T00:00:00",
"noOfPayinyingIntrestOnly":1,
"loanID":1
}
]
}
]
}
]
一种方法与之前一样,您可以 select 新的 Customer 实例并为它一个一个地设置值。
另一种更简单的方法,您可以将 属性 更改为以下以计算 RePaidIn
:
using System.Linq; //import this namespace...
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
private decimal _TotalRepaidIn;
[NotMapped]
public decimal TotalRepaidIn
{
get { return loansHistories.Sum(sum => sum.RePaidIn); }
set { _TotalRepaidIn = value; }
}
//other properties
public ICollection<LoansHistories> loansHistories { get; set; } = new List<LoansHistories>();
}
并且在你的 LoansController
中,你可以将操作改进为以下:
var data = await _context.loans.Include(lh => lh.loansHistories).ToListAsync();
Customer Controller 也可以获得 TotalRepaidIn
的正确值。
我有三个 table Customer
、Loan
和 LoanHistories
。 Customer
table与Loan
table是一对多关系,Loan
table与[=18有关=] 在一对多关系中也是如此。
以下是这些 table 的 C# 类:
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "ID NO")]
public int CustomerID { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Phone Number")]
public string PhoneNo { get; set; }
[Display(Name = "Nearest Primary School")]
public string NearestPrimarySchool { get; set; }
[Display(Name = "Photo")]
public string Photo { get; set; }
public ICollection<Loan> loans { get; set; }
// public IEnumerable<LoansHistories> loansHistories { get; set; }
// public ICollection<WorkForLiving> workForLivings { get; set; }
// public ICollection<CustomersCharacterBehavior> customersCharacterBehaviors { get; set; }
}
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
[NotMapped]
public decimal TotalRepaidIn { get; set; }
[NotMapped]
public decimal Balance { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Loan Balance")]
private decimal _LoanBalance;
public decimal LoanBalance
{
get { return LoanAmount * interestRate; }
set { _LoanBalance = value; }
}
[Display(Name = "Interest Rate")]
public decimal interestRate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Application Date")]
public DateTime ApplicationDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Disbursement Date")]
public DateTime DisbursmentDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Due Date")]
public DateTime DueDate { get; set; }
[Display(Name = "Defaulted")]
public bool Defaulted { get; set; }
[Display(Name = "Approved")]
public bool Approved { get; set; }
//Navigation property
public int CustomerID { get; set; }
public Customer customer { get; set; }
//public ICollection<LoanComments> loancomments { get; set; }
public ICollection<LoansHistories> loansHistories { get; set; }
}
public class LoansHistories
{
[Key]
public int HistID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Repaid Amount")]
public decimal RePaidIn { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Repayement Date")]
public DateTime RepayementDateDate { get; set; }
[Display(Name = "No of paying interest only")]
public int NoOfPayinyingIntrestOnly { get; set; }
// Navigation properties
public int LoanID { get; set; }
public Loan loan { get; set; }
// Navigation property
// public int CustomerID { get; set; }
// public Customer customer { get; set; }
}
这是从 Loans
和 LoansHistories
加载数据的 LoansController
tables:
[HttpGet]
public async Task<ActionResult<IEnumerable<Loan>>> Getloans()
{
var data = await _context.loans
.Include(lh => lh.loansHistories)
.Select(l => new Loan()
{
TotalRepaidIn = l.loansHistories.Select(lh => lh.RePaidIn).Sum(),
Balance = l.loansHistories.Select(lh => lh.RePaidIn).Sum()-l.LoanAmount,
loansHistories = l.loansHistories,
ApplicationDate = l.ApplicationDate,
Defaulted = l.Defaulted,
DisbursmentDate = l.DisbursmentDate,
DueDate = l.DueDate,
LoanID = l.LoanID,
Approved = l.Approved,
interestRate = l.interestRate,
LoanAmount = l.LoanAmount
}).ToListAsync();
return data;
}
这是JSON格式的数据:
[
{
"loanID":1,
"loanAmount":1000.0000,
"totalRepaidIn":202700.0000,
"balance":201700.0000,
"loanBalance":15000.000000,
"interestRate":15.00,
"applicationDate":"2022-03-28T00:00:00",
"disbursmentDate":"2022-03-28T00:00:00",
"dueDate":"2022-04-28T00:00:00",
"defaulted":false,
"approved":true,
"customerID":0,
"customer":null,
"loansHistories":[
{
"histID":1,
"rePaidIn":500.0000,
"repayementDateDate":"2022-03-28T00:00:00",
"noOfPayinyingIntrestOnly":1,
"loanID":1,
"loan":null
}
]
}
]
我想从客户控制器加载数据,但我发现实体 TotalRepaidIn 为零,我怎样才能使它 return 一个值在贷款控制器中有其 returning?没有实体 totalRepaidIn":0.0
客户控制员:
// GET: api/CustomersApi
[HttpGet]
public async Task<ActionResult<IEnumerable<Customer>>> Getcustomers()
{
return await _context.customers.Include(l=>l.loans).ThenInclude(h=>h.loansHistories).ToListAsync();
}
return以下Json数据:
[
{
"customerID":30290122,
"firstName":"Isaac",
"lastName":"Kiplagat",
"phoneNo":"0724797768",
"nearestPrimarySchool":"Mokwo",
"photo":"photo",
"loans":[
{
"loanID":1,
"loanAmount":1000.0000,
"totalRepaidIn":0.0,
"balance":0.0,
"loanBalance":15000.000000,
"interestRate":15.00,
"applicationDate":"2022-03-28T00:00:00",
"disbursmentDate":"2022-03-28T00:00:00",
"dueDate":"2022-04-28T00:00:00",
"defaulted":false,
"approved":true,
"customerID":30290122,
"loansHistories":[
{
"histID":1,
"rePaidIn":500.0000,
"repayementDateDate":"2022-03-28T00:00:00",
"noOfPayinyingIntrestOnly":1,
"loanID":1
}
]
}
]
}
]
一种方法与之前一样,您可以 select 新的 Customer 实例并为它一个一个地设置值。
另一种更简单的方法,您可以将 属性 更改为以下以计算 RePaidIn
:
using System.Linq; //import this namespace...
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
private decimal _TotalRepaidIn;
[NotMapped]
public decimal TotalRepaidIn
{
get { return loansHistories.Sum(sum => sum.RePaidIn); }
set { _TotalRepaidIn = value; }
}
//other properties
public ICollection<LoansHistories> loansHistories { get; set; } = new List<LoansHistories>();
}
并且在你的 LoansController
中,你可以将操作改进为以下:
var data = await _context.loans.Include(lh => lh.loansHistories).ToListAsync();
Customer Controller 也可以获得 TotalRepaidIn
的正确值。