locked
How can I specify a one-to-one relationship with both ends optional? RRS feed

  • Question

  • I've got a simple Teacher-Class relationship here to model. Here's my code:

     

    public class Teacher
     {
      public int Id { get; set; }
      public string Name { get; set; }
      public int? ClassId { get; set; }
      public virtual Class Class { get; set; }
     }
     public class Class
     {
      public int Id { get; set; }
      public string Name { get; set; }
      public int? TeacherId { get; set; }
      public virtual Teacher Teacher { get; set; }
     }
     public class TestContext : DbContext
     {
      public DbSet<Teacher> Teachers { get; set; }
      public DbSet<Class> Classes { get; set; }
     }
    

     


    However, I'm getting this error and can't make it work anyway:

    Unable to determine the principal end of an association between the types 'ConsoleApplication1.Class' and 'ConsoleApplication1.Teacher'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

     

    What should I do with this seemingly simple relationship?

    Thanks,

    Nico


    You Can't Stop Me!
    Tuesday, August 9, 2011 4:59 PM

Answers

All replies