Knowing that a PK can be a FK at the same time, I want to make use of the constraints available in EF core to make this possible for the below entities (models) in C#:
public class ProjectDetailsModel
{
[Key, ForeignKey("ProjectModel"), Unique, Required]
public int ProjectId { get; set; }
[Unique, Required]
public string Context { get; set; }
[Required]
public DateTime StartDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
public IEnumerable<MediaModel> Media { get; set; }
}
public class ProjectModel
{
[Key, Unique, AutoIncrement, Required]
public int Id { get; set; }
[Unique, Required]
public string Title { get; set; }
[Unique, Required]
public string Brief { get; set; }
[ForeignKey("MediaModel"), Unique, Required]
public int ProjectCoverId { get; set; }
// navigation property
public MediaModel Media { get; set; }
}
To confirm the above, when adding migrations, the FK constrain is not generated:
constraints: table =>
{
table.PrimaryKey("PK_ProjectDetailsModel", x => x.ProjectId);
});