如何在 ASP.NET 核心 API 控制器中获取多个 table 的记录
How to fetch record of more than one table in ASP.NET Core API Controller
我想从数据库中获取 3 table,只是 3 table 的所有值。
这个 api 在销售控制器中,我想在其中获取 3 tables 客户、产品和商店,以便我可以在表单中使用它们。
这是数据库上下文
namespace CRUDReact.Model
{
public class DataBaseContext: DbContext
{
public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options) { }
public DbSet<Customer> Customer { get; set; }
public DbSet<Sales> Sales { get; set; }
public DbSet<Product> Product { get; set; }
public DbSet<Store> Store { get; set; }
}
}
这是我想用来获取 3 table 的 api。此 api 在销售控制器中。
// GET: api/AllSales
[HttpGet]
public async Task<ActionResult<IEnumerable<Object>>> GetAllSales()
{
var salesdata = await _context.Sales
.Include(s => s.Customer)
.Include(s => s.Product)
.Include(s => s.Store)
.Select(s => new
{
salesId = s.SalesId,
dateSold = s.DateSold,
customer = new
{
customerId = s.CustomerRefId,
name = s.Customer.Name,
address = s.Customer.Address
},
product = new
{
productId = s.ProductRefId,
name = s.Product.Name,
price = s.Product.Price
},
store = new
{
storeId = s.StoreRefId,
name = s.Store.Name,
address = s.Store.Address
}
})
.ToListAsync();
return salesdata;
}
这是销售额 class
namespace CRUDReact.Models
{
public class Sales
{
[Key]
public int SalesId { get; set; }
public int ProductRefId { get; set; }
[ForeignKey("ProductRefId")]
public Product Product { get; set; }
public int CustomerRefId { get; set; }
[ForeignKey("CustomerRefId")]
public Customer Customer { get; set; }
public int StoreRefId { get; set; }
[ForeignKey("StoreRefId")]
public Store Store { get; set; }
[DataType(DataType.Date)]
public string DateSold { get; set; }
}
}
使用 Include 获取销售额将导致从子表中获取数据。你为什么要投影它。
var salesdata = await _context.Sales
.Include(s => s.Customer)
.Include(s => s.Product)
.Include(s => s.Store).ToListAsync();
通常Sale也会有Store, Customer和List of Products,你确定你的型号是正确的吗
我想从数据库中获取 3 table,只是 3 table 的所有值。
这个 api 在销售控制器中,我想在其中获取 3 tables 客户、产品和商店,以便我可以在表单中使用它们。
这是数据库上下文
namespace CRUDReact.Model
{
public class DataBaseContext: DbContext
{
public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options) { }
public DbSet<Customer> Customer { get; set; }
public DbSet<Sales> Sales { get; set; }
public DbSet<Product> Product { get; set; }
public DbSet<Store> Store { get; set; }
}
}
这是我想用来获取 3 table 的 api。此 api 在销售控制器中。
// GET: api/AllSales
[HttpGet]
public async Task<ActionResult<IEnumerable<Object>>> GetAllSales()
{
var salesdata = await _context.Sales
.Include(s => s.Customer)
.Include(s => s.Product)
.Include(s => s.Store)
.Select(s => new
{
salesId = s.SalesId,
dateSold = s.DateSold,
customer = new
{
customerId = s.CustomerRefId,
name = s.Customer.Name,
address = s.Customer.Address
},
product = new
{
productId = s.ProductRefId,
name = s.Product.Name,
price = s.Product.Price
},
store = new
{
storeId = s.StoreRefId,
name = s.Store.Name,
address = s.Store.Address
}
})
.ToListAsync();
return salesdata;
}
这是销售额 class
namespace CRUDReact.Models
{
public class Sales
{
[Key]
public int SalesId { get; set; }
public int ProductRefId { get; set; }
[ForeignKey("ProductRefId")]
public Product Product { get; set; }
public int CustomerRefId { get; set; }
[ForeignKey("CustomerRefId")]
public Customer Customer { get; set; }
public int StoreRefId { get; set; }
[ForeignKey("StoreRefId")]
public Store Store { get; set; }
[DataType(DataType.Date)]
public string DateSold { get; set; }
}
}
使用 Include 获取销售额将导致从子表中获取数据。你为什么要投影它。
var salesdata = await _context.Sales
.Include(s => s.Customer)
.Include(s => s.Product)
.Include(s => s.Store).ToListAsync();
通常Sale也会有Store, Customer和List of Products,你确定你的型号是正确的吗