Home > Mobile >  understanding entity framework core foreign key relationships
understanding entity framework core foreign key relationships

Time:11-30

Im utilsing a code first approach for the first time (Ive previously always used database first) and am trying to understand some basic concepts. If I create a foreign key relationship between two entities, how does entity framework know which properties (columns) to use in the two sides of the relationship ? My question is probably better explained with a simple code example. I have two entities, patient and treatment. A patient can have multiple treatments so there will be a one to many relationship between the patient and the treatment, with a foreign key relationship existing between the two entities. Here are my entity classes. Please note these are greatly simplified for the sake of explanation.

public class Patient
{
    public int Id { get; set; }
    public string FirstName { get; set; }
   
    public string LastName { get; set; }

    public ICollection<PatientTreatment> PatientTreatment { get; set; }
}

public class PatientTreatment
{
    public int Id { get; set; }
   
    public string TreatmentDescription { get; set; }

    public int PatientId { get; set; }
    public virtual Patient Patient { get; set; }
}

So for the patient entity the primary key would be Id and for the PatientTreatment entity, its primary key would also be Id

For the foreign key relationship, according to what Ive googled so far, the code above will create that relationship for me, is this correct ? If so, how would entity framework know that the PatientId in PatientTreatment is linked to Id in the Patient entity ? This is how its supposed to be in the database (SQL Server), but I cant see how entity framework would know this. Im really new to the code first approach so Im just trying to understand how this would work. Could anyone explain this to me ? Ive also read that setting the relationship as above doesnt create indexes (PatientId in PatientTreatment) so these have to be created in code as well

CodePudding user response:

EF works with conventions, as Caius mentioned.

In your case:

  • EF knows that there are two entity object - Patient and PatientTreatment, because dbSet and optional configuration exist for those classes.
  • Patient contains so called navigation property leading to PatientTreatment's - a collection, but it could be most of the things implementing IEnumerable - EF assumes that You want to create relationship here.
  • Patient have an Id field - EF by naming convention without any configuration will assume that this is an entity key. Same goes for PatientTreatment
  • PatientTreatment has a navigation property to a single Patient - this, again, by convention tells EF that you want the relationship between this two entities to be one-to-many - collection on one side, single reference on the other side.

Ofc one to many could also be possible by convention even without navigation property in PatientTreatment - just to be clear.

  • Related