为什么我从数据库中获取空对象?
我有一个具有属性的发票对象,其中有一个客户端对象和一个项目列表。我的目标是从数据库中检索发票列表,但每次我这样做时,发票都会返回分配给其客户端对象和项目列表的空引用。我的数据模型有问题吗?
这是我的数据模型:
public class Invoice
{
public string InvoiceId { get; set; } = GenerateID.GenerateInvoiceID();
public string Description { get; set; }
public List<InvoiceItem> Items { get; set; }
public DateTime InvoiceDate { get; set; }
public string PaymentTerms { get; set; }
public DateTime PaymentDue { get; set; }
public int TotalPrice { get; set; }
public string Status { get; set; } = "pending";
public Client Client { get; set; }
public string BillFromAddress { get; set; }
public string BillFromCity { get; set; }
public string BillFromCountry { get; set; }
public string BillFromPostal { get; set; }
}
public class Client
{
public string ClientId{ get; set; } = GenerateID.GenerateClientID();
[Required]
public string ClientName { get; set; }
[Required]
public string ClientEmail { get; set; }
[Required]
public string ClientAddress { get; set; }
[Required]
public string ClientCity { get; set; }
[Required]
public string ClientCountry { get; set; }
[Required]
public string ClientPostal { get; set; }
}
public class InvoiceItem
{
public string InvoiceItemId { get; set; } = GenerateID.GenerateItemID();
public string InvoiceId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public int Price { get; set; }
public InvoiceItem()
{
}
public InvoiceItem(string itemName, int quantity, int price)
{
Name = itemName;
Quantity = quantity;
Price = price;
}
}
这是添加和检索发票的服务:
public async void AddInvoice(InputModel input)
{
Invoice invoice = new Invoice();
invoice.Description = input.Description;
invoice.Items = input.Items;
invoice.InvoiceDate = input.InvoiceDate;
invoice.PaymentTerms = input.PaymentTerms;
invoice.Client = input.Client;
invoice.BillFromAddress = input.BillFromAddress;
invoice.BillFromCity = input.BillFromCity;
invoice.BillFromCountry = input.BillFromCountry;
invoice.BillFromPostal = input.BillFromPostal;
//Attaching the invoice id to each item in the invoice
foreach (var item in invoice.Items)
{
item.InvoiceId = invoice.InvoiceId;
}
_context.Add(invoice);
await _context.SaveChangesAsync();
}
public async Task<List<Invoice>> GetInvoices()
{
return await _context.Invoices.ToListAsync();
}
这是 DbContext 类:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Invoice> Invoices { get; set; }
}
这是数据库中的记录:
I have an invoice object that has properties, among them a client object and a list of items. My goal is to retrieve a list of invoices from the database but every time I do that the invoices come back with null references assigned to their client objects and lists of items. Is there something wrong with my data model ?
Here's my data model:
public class Invoice
{
public string InvoiceId { get; set; } = GenerateID.GenerateInvoiceID();
public string Description { get; set; }
public List<InvoiceItem> Items { get; set; }
public DateTime InvoiceDate { get; set; }
public string PaymentTerms { get; set; }
public DateTime PaymentDue { get; set; }
public int TotalPrice { get; set; }
public string Status { get; set; } = "pending";
public Client Client { get; set; }
public string BillFromAddress { get; set; }
public string BillFromCity { get; set; }
public string BillFromCountry { get; set; }
public string BillFromPostal { get; set; }
}
public class Client
{
public string ClientId{ get; set; } = GenerateID.GenerateClientID();
[Required]
public string ClientName { get; set; }
[Required]
public string ClientEmail { get; set; }
[Required]
public string ClientAddress { get; set; }
[Required]
public string ClientCity { get; set; }
[Required]
public string ClientCountry { get; set; }
[Required]
public string ClientPostal { get; set; }
}
public class InvoiceItem
{
public string InvoiceItemId { get; set; } = GenerateID.GenerateItemID();
public string InvoiceId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public int Price { get; set; }
public InvoiceItem()
{
}
public InvoiceItem(string itemName, int quantity, int price)
{
Name = itemName;
Quantity = quantity;
Price = price;
}
}
Here are the services that add and retrieve invoices:
public async void AddInvoice(InputModel input)
{
Invoice invoice = new Invoice();
invoice.Description = input.Description;
invoice.Items = input.Items;
invoice.InvoiceDate = input.InvoiceDate;
invoice.PaymentTerms = input.PaymentTerms;
invoice.Client = input.Client;
invoice.BillFromAddress = input.BillFromAddress;
invoice.BillFromCity = input.BillFromCity;
invoice.BillFromCountry = input.BillFromCountry;
invoice.BillFromPostal = input.BillFromPostal;
//Attaching the invoice id to each item in the invoice
foreach (var item in invoice.Items)
{
item.InvoiceId = invoice.InvoiceId;
}
_context.Add(invoice);
await _context.SaveChangesAsync();
}
public async Task<List<Invoice>> GetInvoices()
{
return await _context.Invoices.ToListAsync();
}
Here’s the DbContext class:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Invoice> Invoices { get; set; }
}
Here are the records in the database:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了能够查询
Invoice
的Client
、InvoiceItem
等相关实体,您需要预先加载。参考资料
预加载
To be able to query the related entities such as
Client
,InvoiceItem
ofInvoice
, you need eager loading.References
Eager Loading