The following code gives the desired circuit. Note that you also need to define the foreign key ParentTaskIDas an integer with a null value, as shown below.
public class Task
{
public int TaskID { get; set; }
public string TaskName { get; set; }
public int? ParentTaskID { get; set; }
public Task ParentTask { get; set; }
}
public class Context : DbContext
{
public DbSet<Task> Tasks { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Task>()
.HasOptional(t => t.ParentTask)
.WithMany()
.HasForeignKey(t => t.ParentTaskID);
}
}
source
share