You configure your relationships by convention:
https://www.entityframeworktutorial.net/efcore/conventions-in-ef-core.aspx
Therefore the naming is important Change the foreign Keys in the Task Model:
public class Task
{
public int Id { get; set; }
public int TaskTypeId { get; set; }
public int Child1Id { get; set; }
public Child1 Child1{ get; set; }
public int Child2Id { get; set; }
public Child2 Child2{ get; set; }
}
Alternative solution, relationship by Fluent API configuration:
https://www.entityframeworktutorial.net/efcore/fluent-api-in-entity-framework-core.aspx
With IEntityTypeConfiguration.
public class Task
{
public int Id { get; set; }
public int ChildId { get; set; }
public int TaskTypeId { get; set; }
public Child1 Child1{ get; set; }
public Child2 Child2{ get; set; }
}
public class Child1
{
public int Id { get; set; }
public Task Task{get; set;}
}
public class Child2
{
public int Id { get; set; }
public Task Task {get; set;}
}
The implementtion of IEntityTypeConfiguration:
internal class Child1DbConfig : IEntityTypeConfiguration<Child1>
{
public void Configure(EntityTypeBuilder<Child1> entity)
{
entity.ToTable("Child1");
entity.HasKey(c => c.Id);
entity.HasOne(d => d.Task)
.WithOne(p => p.Child1)
.HasForeignKey(d => d.TaskId);
}
}
Dont forget to make a new migration after adding/editing the IEntityTypeConfiguration interface.
Also: Maybe choose another name for your Task class. Task is already a Class name in the 'System.Threading' namespace. So if you use anything async, you will run in to trouble sooner or later.
Thanks for CodeCaster for correcting my (wrong) Answer!