aspnet core entity framework 7 self referencing "job" 1 to many table

c# ef-fluent-api entity-framework entity-framework-core self-reference


I have a "Job" table that contains jobs. The fact is Jobs are not always done in one go.. you can have a job that has many visits. I intended to represent that as another job but linked back to the original job via self referencing linkId.

I am having trouble representing this using the fluent API. Its a one to many relationship.. one job might have many visits and thus a number of linkId's point back to the orginal job. The link Id would back to the orginal job Id. Its also optional since most jobs might be completed in one go..

I have looked for this but its difficult to turn the other examples to this example as alot of them are one to one and further the ones that give examples of one to many seem to do so using EF6 which is different.

My job table is:

    using System;

namespace JobsLedger.Model.Entities
    public class Job : IEntityBase
        public int Id { get; set; }
        public string Model { get; set; }
        public string Serial { get; set; }
        public string ProblemDetails { get; set; }
        public string SolutionDetails { get; set; }
        public DateTime JobDate { get; set; }
        public int BrandId { get; set; }
        public int JobTypeId { get; set; }
        public int StatusId { get; set; }
        public int ClientId { get; set; }
        public int UserId { get; set; }

        public int? LinkId { get; set; }  //If there are more than one job callout eg back to fit parts.
        public Job MultipleJobVisits { get; set; }

I am pretty sure I have this wrong:

        // Job
        modelBuilder.Entity<Job>().Property(j => j.Model).HasMaxLength(100);
        modelBuilder.Entity<Job>().Property(j => j.Serial).IsRequired();
        modelBuilder.Entity<Job>().Property(j => j.ProblemDetails).HasMaxLength(100);
        modelBuilder.Entity<Job>().Property(j => j.SolutionDetails).HasMaxLength(500);
        modelBuilder.Entity<Job>().Property(j => j.JobDate);
        modelBuilder.Entity<Job>().Property(j => j.Notes).HasMaxLength(1000);
        modelBuilder.Entity<Job>().Property(j => j.BrandId);
        modelBuilder.Entity<Job>().Property(j => j.JobTypeId);
        modelBuilder.Entity<Job>().Property(j => j.StatusId);
        modelBuilder.Entity<Job>().Property(j => j.ClientId);
        modelBuilder.Entity<Job>().Property(j => j.UserId);
        modelBuilder.Entity<Job>().HasOne(x => x.Id)
                                  .WithMany(x => x.LinkId)
                                  .ForeignKey(x => x.Id)

How do I represent a one to many self reference in EF7 and fluent API and which is optional?

EDIT: whilst this doesnt provide a syntax error I have to say I am not sure if its OK

modelBuilder.Entity<Job>().HasMany(j => j.LinkedJobs).WithOne().IsRequired(false);

Any help on this is appreciated... I have found precious little in the way of knowledge on how to configure a one to many self referencing relationship...

11/15/2016 11:52:28 AM

Accepted Answer

Job class:

public class Job
    public int Id { get; set; }

    public int? JobId { get; set; }

    public Job ParentJob { get; set; }

    public ICollection<Job> ChildJobs { get; set; }

Fluent API:

                .HasMany(oj => oj.ChildJobs)
                .WithOne(j => j.ParentJob)
                .HasForeignKey(j => j.JobId);
11/15/2016 1:03:24 PM

Related Questions


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