【问题标题】:ASP.NET Core & Entity Framework Core : one-to-many relation and navigation propertiesASP.NET Core 和 Entity Framework Core:一对多关系和导航属性
【发布时间】:2020-06-27 23:38:00
【问题描述】:

我正在使用 ASP.NET Core 和 Entity Framework Core,以及用于我的 react 应用数据库连接的控制器 API。

我有 4 个班级 CustomerProductStoreSalesCustomerProductStore 表与销售额是一对多的关系。

销售类

public class Sales
{
    [Key]
    public int SalesId { get; set; }
    public int ProductId { get; set; }
    public int CustomerId { get; set; }
    public int StoreId { get; set; }
    [DataType(DataType.Date)]
    public string DateSold { get; set; }

    public Product Product { get; set; }
    public Customer Customer { get; set; }
    public Store Store { get; set; }
}

客户类别

public class Customer
{
    [Key]
    public int CustomerId { get; set; }
    [Column(TypeName = "nvarchar(100)")]
    public string Name { get; set; }
    [Column(TypeName = "nvarchar(100)")]
    public string Address { get; set; }

    public IList<Sales> Sales { get; set; }
}

其他产品和商店与客户类相同。

我运行了迁移命令,但没有创建数据库并且命令运行成功,所以我创建了数据库,然后我运行了 update-database,它在数据库中创建了所有表。

如果我将导航属性添加到表中,它将根据销售记录中的 ID 获取包含客户、产品和商店记录的销售记录。

我想获取销售记录,在销售表中有客户、产品和商店 ID。如何获取他们的记录?

我的表格如下所示:

【问题讨论】:

  • 有几种方法可以做到这一点,但我喜欢使用外键,它将 id 绑定到“虚拟”表:entityframeworktutorial.net/code-first/…(你应该让整数可以为空......所以"public int?ProductId { get; set; }")
  • @Dale K 我确实在依赖实体的导航属性上尝试了这个 [ForeignKey],但它没有创建导航属性。我会试试校长
  • @Dale K 我在依赖实体的导航属性上尝试了 [ForeignKey],现在我在销售表中有外键,但没有导航属性
  • 我认为您的意思是标记@pcalkins,因为我没有为这个问题做出贡献。
  • 哦对不起@pcalkins 和@Dale K

标签: sql-server asp.net-core entity-framework-core


【解决方案1】:

我想获取销售记录,在销售表中有客户、产品和商店 ID。如何获取他们的记录?

根据您的模型设计,您可以使用Include 方法加载相关数据,如下所示:

控制器

    [HttpGet]
    public async Task<ActionResult<object>> GetSales() 
    {
        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
                        {
                            name = s.Customer.Name,
                            address = s.Customer.Address
                        },
                        product = new
                        {
                            name = s.Product.Name,
                            price = s.Product.Price
                        },
                        store = new
                        {
                            name = s.Store.Name,
                            address = s.Store.Address
                        }
                    })
                    .ToListAsync();
        return salesdata; 
    }

对于查询语法,您可以在 EF Core 中为复杂查询运算符使用 Join

var data = from s in _context.Sales
                   join cu in _context.Customer on s.CustomerId equals cu.CustomerId 
                   join p in _context.Product on s.ProductId equals p.ProductId 
                   join st in _context.Store on s.StoreId equals st.StoreId 
                   select new
                   {
                       salesId = s.SalesId,
                       dateSold = s.DateSold,
                       customer = new
                       {
                           name = s.Customer.Name,
                           address = s.Customer.Address
                       },
                       product = new
                       {
                           name = s.Product.Name,
                           price = s.Product.Price
                       },
                       store = new
                       {
                           name = s.Store.Name,
                           address = s.Store.Address
                       }
                   };
        return await data.ToListAsync();     

结果

【讨论】:

  • 我正在使用 API 控制器,在获取 API 时看起来像这样
  • // GET: api/Sales [HttpGet] public async Task>> GetSales() { return await _context.Sales.ToListAsync(); }
  • @RahulWaghmare,请查看我的更新。如果您发现我的解决方法有效,您能否将我的回复标记为答案?
  • 非常感谢。是的,我已将您的回复标记为答案。
猜你喜欢
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 2023-02-09
  • 2017-02-17
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2017-01-01
相关资源
最近更新 更多