4

Using the newest Entity Framework I have a class with a one-to-many wit only one navigation property on the many side.

As stated in MSDN: Entity Framework Fluent API - Relationships:

A one-directional (also called unidirectional) relationship is when a navigation property is defined on only one of the relationship ends and not on both.

Simplified: a School has many Students; there is a one-to-many relation between School and Student, but the School doesn't have a property containing the collection of Students

class Student
{
    public int Id {get; set;}
    // a Student attends one School; foreign key SchoolId
    public int SchoolId {get; set;}
    public School School {get; set;}
}

class School
{
    public int Id {get; set;}
    // missing: public virtual ICollection<Studen> Students {get; set;}
}

In a two-directional relationship, you could write the following fluent API in OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Student>()
        .HasRequired(student => student.School)
        .WithMany(school => school.Students)
        .HasForeignKey(student => student.SchoolId);
}

Because of the lack of School.Students, I need to do something extra. According to the link at the beginning it seems that I'd have to do something with WithRequiredDependant.

// Summary:
//     Configures the relationship to be required without a navigation property
//     on the other side of the relationship. The entity type being configured will
//     be the dependent and contain a foreign key to the principal. The entity type
//     that the relationship targets will be the principal in the relationship.
//
public ForeignKeyNavigationPropertyConfiguration WithRequiredDependent();

modelBuilder.Entity<Student>()
    .HasRequired(student => student.School)
    .WithRequiredDependent();

Alas, this doesn't work. SchoolId is not modeled as the foreign key.

What fluent API do I need?

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • Did you try `WithRequiredPrincipal` instead of `WithRequiredDependent`? – Fabiano Jul 13 '17 at 08:58
  • "Using the newest ..." - better be clear about the exact versions and if it's Core or not. This question should still mean something in a few months. – H H Jul 13 '17 at 09:05
  • Of course I tried both WithRequired..., I didn't mention that, because question was already so long. With "the newest" I meant that I am prepared to update to whatever version I need, to prevent answers like: "if you would upgrade to version ... you could use ..." – Harald Coppoolse Jul 13 '17 at 09:06

1 Answers1

5

I hope I have the right version/edition in mind:

modelBuilder.Entity<Student>()
    .HasRequired(student => student.School)
  //.WithMany(school => school.Students)
    .WithMany()
    .HasForeignKey(student => student.SchoolId);
H H
  • 263,252
  • 30
  • 330
  • 514
  • That's it: WithMany() without parameters! It has nothing to do with RequiredPrincipal and RequiredDependant. I found out that is used in a required:required relation (is that one-to-one?) – Harald Coppoolse Jul 13 '17 at 09:05