【问题标题】:Unable to determine the relationship represented by navigation property EF Core无法确定导航属性 EF Core 表示的关系
【发布时间】:2021-07-29 07:28:16
【问题描述】:

我将这些类作为我的领域模型:

public class Ticket
{
    public Guid Id { get;  set; }
    public Guid ParentId { get;  set; }
    public string Subject { get;  set; }
    public string Body { get;  set; }
    public string TicketStatus { get;  set; }
    public User SenderUserId { get;  set; }
    public User ReciverrUserId { get;  set; }
    public DateTime SubmitDate { get; set; }
}

public class User
{
        public Guid Id { set; get; }
        public DateTime SubmitDate { set; get; }

        public string Fullname { set; get; }
        public string Permission { set; get; }
        public string Role { set; get; }//admin/agent/merchant
        public string Password { set; get; }

        public string NationalCode { set; get; }
        public string Email { set; get; }
        public string Telephone { set; get; }
        public string Mobile { set; get; }
        public string Address { set; get; }

        public CountrySection Provience { set; get; }
        public CountrySection City { set; get; }
        //public Department Department { set; get; }

        public CountrySection Town { set; get; }

        public ICollection<Merchant> Stores { set; get; }
        public ICollection<Invoice> Invoices { set; get; }
        public ICollection<Ticket> Tickets { set; get; }
}

当我添加迁移时,我收到此错误:

无法确定“用户”类型的导航“Ticket.SenderUserId”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性

【问题讨论】:

  • 您是否尝试按照错误提示进行操作?
  • 尝试遵循 EF 所期望的命名约定:SenderUserId -> SenderUser, ReciverrUserId -> ReceiverUser
  • @abdusco 我按你说的改了名字,但同样的错误
  • DbContext.OnModelConfiguring有配置吗?
  • @abdusco 不,我没有

标签: c# entity-framework asp.net-core .net-core entity-framework-core


【解决方案1】:

我在 Ticket 类中看到 2 个用户属性,但 User 中只有一个 Ticket 集合,尝试通过添加另一个来修复它

public class Ticket
{
    public Guid Id { get;  set; }
  .....
 public int SenderUserId { get;  set; }

   [ForeignKey(nameof(SenderUserId ))]
    [InverseProperty(nameof(User.SenderUserTickets))]
    public User SenderUser { get;  set; }
    
      public int ReciverrUserId { get;  set; }

    [ForeignKey(nameof(ReciverrUserId))]
    [InverseProperty(nameof(User.ReciverrUserTickets))]
    public User ReciverrUser { get;  set; }
   

public class User
{
        public Guid Id { set; get; }
        .....
        [InverseProperty(nameof(Ticket.SenderUser ))]
        public ICollection<Ticket> SenderUserTickets { set; get; }
         [InverseProperty(nameof(Ticket.ReciverrUser ))]
        public ICollection<Ticket> ReciverrUserTickets { set; get; }
}

抱歉,我使用属性是因为我喜欢将所有内容都放在一个地方。您可以将它们翻译成流利的 api。我也添加了几个 Id,但它是可选的,EF 可以创建阴影。

【讨论】:

  • 我从来没有因为提倡外键关联而被否决。它甚至是使用 EF 的首选方式,让很多事情变得容易得多。
  • @GertArnold 谢谢。你知道我真的很感激你的意见。只是有些大师不这么认为。
  • @GertArnold 我更新了我的答案。我希望现在更好。
  • 这样做是很常见的做法。有些人(大师?)将 EF 类模型用于域模型(如在 DDD 中),而它只不过是一个数据访问层,完全致力于该任务。那些试图将其用作领域模型的人忘记了“单一职责”之类的东西,立即取消了自己作为 DDD 专家的资格。
  • @IMHO EF 团队制作阴影属性并不是最好的主意,尤其是 Id 和多对多类。有很多新手开发者因此不明白不可能将整个类实例保存到数据库中,只能保存一列,通常是主键。在许多情况下,阴影使查询变得更加复杂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多