How to map parent=>children without navigation property on child

c# entity-framework-core

Question

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?

1
0
1/22/2019 1:26:11 PM

Popular Answer

Read more here: Single Navigation Property

modelBuilder.Entity<Parent>()
    .HasMany(p => p.Children)
    .WithOne();
0
1/22/2019 1:38:14 PM


Related Questions





Related

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