0

I'm using entity framework core. I want to have 3 tables: Appoitments, Users and Roles. The 'Users' table contains application members with different roles. Is there a way to have 'Appoitments' table with foreign keys: ClientID and ConsultantID of type 'User'?

public class ApplicationUser : IdentityUser
{
    public List<Appointment> Appointments { get; set; }
}

public class Appointment
{
    public int AppointmentId {get;set;}
    public DateTime Date { get; set; }
    public int RoomNumber { get; set; }

    public ApplicationUser ConsultantId { get; set; }
    public ApplicationUser ClientId { get; set; }
}

I created classes ApplicationUser and Appointment and in ApplicationDbContext i added property: public DbSet Appointments { get; set; }.

But when I tried to add migration, Packet Manager threw "(Unable to determine the relationship represented by navigation property 'ApplicationUser.Appointments' of type 'List'.

KrzT
  • 27
  • 1
  • 7
  • 1
    Yes, I don't see why there couldn't be? What have you tried? Has anything gone wrong? – Grizzly May 22 '18 at 12:24
  • Better you go through https://learn.microsoft.com/en-us/ef/core/modeling/relationships – Vivek Nuna May 22 '18 at 12:25
  • I created classes ApplicationUser and Appointment and in ApplicationDbContext i added property: public DbSet Appointments { get; set; }. – KrzT May 22 '18 at 12:26
  • Seems similar to [this question](https://stackoverflow.com/questions/38438052/many-to-many-relation-between-identity-and-custom-table-ef7-code-first) – Grizzly May 22 '18 at 12:39
  • Create two collection navigation properties and map them to to corresponding reference navigation property. Also it's good to strip `Id` from the navigation property name. e.g. `ApplicationUser.ClientAppointments` -> `Appointment.Client` and `Application.User.ConsultantAppointments` -> `Appointment.Consultant`. – Ivan Stoev May 22 '18 at 12:45

1 Answers1

1

Not enough information in the question to really distinguish what is going on.. but I don't see why something like this wouldn't work:

public class Appointment
{
    public int AppointmentId {get;set;}
    public DateTime Date { get; set; }
    public int RoomNumber { get; set; }
    [ForeignKey("Consultant")]
    public int ConsultantId { get; set; }
    [ForeignKey("Client")]
    public int ClientId { get; set; }


    public ApplicationUser Consultant { get; set; }
    public ApplicationUser Client { get; set; }
}
Grizzly
  • 5,873
  • 8
  • 56
  • 109