0

I have a project where I need a complex relationship:

public enum InputFileTypes
{
    unknown = 0,
    ConfirmationFile = 1,
    PrestationFile = 2,
    EOCChoiceFile = 3,
    EOCReplaceFile = 4,
    CareStaffFile = 5,
    JobCreationFile = 6,
    NurseTitleFile = 7
};

public class InputFile
{
    [Key]
    public int Id { get; set; }
    public string Filename { get; set; }
    public InputFileTypes InputFileType { get; set; }
    public Guid ScraperUploadClassId { get; set; }
    public ScraperUploadClass ScraperUploadClass { get; set; }

    public DateTime FileDateTime
    {
        get
        {
            return File.GetCreationTime(Filename);
        }
    }
}

public class ScraperUploadClass
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid ID { get; set; }
    public Regions Region { get; set; }
    public virtual ICollection<InputFile> ConfirmationFiles { get; set; }
    public virtual ICollection<InputFile> PrestationFiles { get; set; }
    public InputFile EocChoiceFile { get; set; }
    public InputFile EocReplaceFile { get; set; }
    public InputFile CareStaffFile { get; set; }
    public InputFile JobCreationFile { get; set; }
    public InputFile NurseTitlesFile { get; set; }
}

modelBuilder.Entity<InputFile>()
    .HasIndex(c => new { c.InputFileType, c.ScraperUploadClassId });

The problem is the ModelBuilder: InputFile has a combined primary key: inputfiletype and the ScraperUploadclassid.

Scraperclass needs multiple relations towards inputfile:

  • one to one for the property EocCHoiceFile where the InputFile.InputFileType == InputFileTypes.EOCChoiceFile
  • one to one for the property EOCReplaceFile where the InputFile.InputFileType == InputFileTypes.EOCReplaceFile

...

  • a one to many for the Confirmationsfiles list where InputFile.InputFileType == ConfirmationFile
  • finally a one to many for PrestationsFiles List where InputFile.InputFileType == PrestationFile

Can anyone please let me know how to do this in the DbContext.OnModelCreating method?

I don't seem to get to it with the modelBuilder.... it always seems to cause problems when adding migrations.

1 Answers1

0

Found the answer muyself, using only one virtual list in ScraperUploadClass and than make getter functions for the separate types

public class InputFile
    {
        [Key]
        public int Id { get; set; }
        public string Filename { get; set; }
        public InputFileTypes InputFileType { get; set; }
        public Guid ScraperUploadClassID { get; set; }
    }
    public class ScraperUploadClass
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid ID { get; set; }
        public Regions Region { get; set; }
        public virtual ICollection<InputFile> InputFiles { get; set; }
    }

            modelBuilder.Entity<InputFile>()
                            .Property(c => c.InputFileType)
                            .HasConversion<string>();

            modelBuilder.Entity<InputFile>()
                .HasOne(d => d.ScraperUploadClass)
                .WithMany(p => p.InputFiles)
                .HasForeignKey(d => d.ScraperUploadClassID);

            modelBuilder.Entity<InputFile>()
             .HasIndex(t => new { t.ScraperUploadClassID, t.InputFileType, t.Filename })
             .IsUnique();