Navigation Properties in EntityFrameworkCore

c# entity-framework-core

Question

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'.

1
0
5/22/2018 12:32:32 PM

Accepted Answer

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; }
}
1
5/22/2018 12:29:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow