EF核心:来自内容实体的可选一对一外键关系

问题描述 投票:-1回答:1

我已经为.net核心blazor应用程序实现了一些数据库上下文。数据库上下文可以访问外部数据库(无数据库迁移等)。

现在,我的问题是当父表包含表的外键时,我不确定如何使用流利的api或data属性定义外键。

作为简化示例:我有一个交易实体,其数据如下:

[Table("transactions")]
public class Transaction
{
  [Key]
  [Column("id")]
  public int Id { get; set; }

  [Column("trans_num")]
  public string TransNum { get; set; }

  [Column("shop_id")]
  public int? ShopId { get; set; }

  [Column("total_amount")]
  public decimal TotalAmount { get; set; }

  public Shop Shop { get; set;}
}

还有一些商店实体,其数据如下:

[Table("shops")]
public class Shop
{
  [Key]
  [Column("id")]
  public int Id { get; set; }

  [Column("shop_name")]
  public string ShopName{ get; set; }

  public Transaction Transaction { get; set;}
}

如模型所示,“ shop_id”是外键。

所以...我的商店实体中没有交易参考。另外,在我的生产场景中,我有一些这样的可选关系,例如,shop_id将为null。

我将如何指示与模型构建器的可选关系?

最诚挚的问候

我已经为.net核心blazor应用程序实现了一些数据库上下文。数据库上下文可以访问外部数据库(无数据库迁移等。)现在我的问题是我不确定...

entity-framework .net-core entity-framework-core data-annotations ef-model-builder
1个回答
0
投票

在模型构建器中设置可选FK

© www.soinside.com 2019 - 2024. All rights reserved.