I am creating a Visual Studios MVC5 Web application and I am trying to reference the id from AspNetUser table as a foreign key.
I am creating a new table :
public class Patient
{
public int PatientId { get; set; }
public string HCN { get; set; }
public string GP { get; set; }
public string MedHis { get; set; }
public string Medication { get; set; }
public string CurrentPrescription { get; set; }
public string PresentRX { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser UserId { get; set; }
}
My Patient config class :
public class PatientConfig : EntityTypeConfiguration<Patient>
{
public PatientConfig()
{
ToTable("Patient");
Property(x => x.PatientId).HasColumnName("IntId");
HasKey(x => x.PatientId);
Property(x => x.HCN).HasColumnName("strHCN");
Property(x => x.GP).HasColumnName("strGP");
Property(x => x.MedHis).HasColumnName("strMedHis");
Property(x => x.Medication).HasColumnName("strMedication");
Property(x => x.CurrentPrescription).HasColumnName("strCurrentPrescription");
Property(x => x.PresentRX).HasColumnName("strPresentRX");
Property(x => x.UserId).HasColumnName("intUserId");
}
}
Firstly I was getting the error
Must be a non-nullable value type in order to use it as a parameter
underneath the UserId property so I removed it
Property(x=> x.UserId).HasColumnName("intUserId");
When I tried to add the migration I got the following error:
The ForeignKeyAttribute on property 'UserId' on type 'ModelTest.Models.Patient' is not valid. The foreign key name 'UserId' was not found on the dependent type 'ModelTest.Models.Patient'. The Name value should be a comma separated list of foreign key property names.
I don't understand how the UserId cannot be found, any advice would be greatly appreciated.
Thanks