Unable to determine the relationship represented by navigation property EF Core

Viewed 136

I have these classes as my domain models:

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; }
}

When I add migration I get this error:

Unable to determine the relationship represented by navigation 'Ticket.SenderUserId' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'

1 Answers

I see 2 user properties in a Ticket class, but only one Ticket collection in User, try to fix it by adding another one

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; }
}

Sorry I am using attributes since I like to keep everything in one place. You can translate them to fluent apis. I added a couple of Ids too, but it is optional, EF can create the shadows.

Related