1

I have 2 classes:

public class office 
{
    [Key]
    public Guid ID { get; set; }

    //...and some more fields...
}

and

public class room 
{
    [Key]
    public Guid ID { get; set; }

    [Foreignkey("office")]
    public Guid ID_office { get; set; }

    //...and some more fields...

    public virtual office office { get; set; }
}

In database context I did the following mapping:

modelBuilder.Entity<room>().HasRequired(n => n.office).WithRequiredPrincipal();

Because I need exactly ONE (required) office for each room. Each office may have rooms (none, one or many) but this relation is not needed in the application. I've tested a lot of variations how to specifiy the relation but always getting errors. What is the correct way to specify this relation?

To make it clearly: I need a required office_id at each room(one-to-one) an no back relation from office to rooms. My problem: with the relations as specified above, Entity Framework always throws an error saying that 'room_id' not found in office model. How to solve this problem?

  • 4
    *Each office may have rooms (none, one or many)* So this is `one-to-many` relationship then. – Ivan Stoev Apr 19 '17 at 13:11
  • 1
    http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx Also - I would suggest either annotations or fluent, but not both. – Steve Greene Apr 19 '17 at 13:12
  • Did you try removing the setting added in database context file. The annotations on the properties would suffice. After removing the line from database context, could you post the error information. Thanks. – sam Apr 19 '17 at 13:13
  • @ivan Stoev: yes, it's one-to-many from office to rooms but as I said: this relation is not needed in my application. I had already seen the tutorial in your link before, but it didn't help. – NightWizzard Apr 19 '17 at 20:46
  • @sam: I removed the fluent API specs from database context but error is the same: EF is mssing the column 'rooms_id' for the back link from offices which does not exists in office model. How to specify this relation properly, only relating one-to-one (required) from room and no relation from office? – NightWizzard Apr 19 '17 at 20:50
  • You might introduce a [junction table](http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table) OfficeRoom and add a boolean property IsRequiredOffice then in your logic insure that only one record is ever set. – Steve Greene Apr 20 '17 at 13:02
  • @Steve Greene: thanks for your suggestion - I still hope there is a simple and direct solution for this task. I can't believe that there is no way in EF to define a simple one-to-one-relation from room and no (or one-to-many) relation from office....? – NightWizzard Apr 20 '17 at 13:24
  • Have you tried `modelBuilder.Entity().HasRequired(n => n.office).WithMany();` ? – Steve Greene Apr 20 '17 at 13:35
  • Yes - same error. Seems I have to redesign the complete datamodel. Much work, because the database worked fine for years with Silverlight and .NET SQL client libraries. EF is fine for simple models but absolutely unusable for really complex and elegant models not following the naming conventions. Grrrrrrrr! – NightWizzard Apr 20 '17 at 16:55
  • OK, solved the problem by complete redesign of the datamodel following the naming conventions of EF and reducing the relation specs to data annontaions only. Thanks to all for their suggestions. – NightWizzard Apr 21 '17 at 09:37

0 Answers0