Currently I'm trying to configure a parent => child relationship with codefirst db generation.
public class Parent
{
public Guid Id{ get; protected set; }
public virtual ICollection<Child> Children { get; protected set; }
public string Name { get; set; }
}
public class Child
{
public Guid Id { get; protected set; }
public string Name { get; protected set; }
}
This is how my entities are configured:
public class ParentConfiguration : IEntityTypeConfiguration<Parent>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.ToTable("Parent");
builder.HasKey(parent => parent .Id);
builder.HasMany(parent => parent.Children);
}
}
public class ChildConfiguration : IEntityTypeConfiguration<Child>
{
public void Configure(EntityTypeBuilder<Child> builder)
{
builder.ToTable("Child");
}
}
It's only allowed to get children via it's parent. A child isn't allowed to know which parent he has.
However entiteframework core is adding a foreign key in the child table to the parent table. Instead I want to have a foreign key from the parent to the child table.
What am I missing?
Read more here: Single Navigation Property
modelBuilder.Entity<Parent>()
.HasMany(p => p.Children)
.WithOne();