3

Motivation : My EF4.1 DbContext is saving Entities in the wrong order

Reason : Lack of navigation properties on my models

How I want to fix it :

I want to set up foreign key relationships in my DbContext. The catch is that my entity objects have no navigation properties (I'm using it to populate a web service and then firing DTO objects over to my application).

The classes below would be an example. In MinorClass, I want to configure my context so that it knows MajorClassID is a foreign key. The articles I've been finding on the internet on how to explicitly define Foreign Keys involve using navigational properties, which my objects dont have.

Is there a way to map this relationship?

public class MinorClass
{
    public Guid ID {get;set:}
    public Guid MajorClassID {get;set;} // foreign key
    public string Name {get;set;}
}

public class MajorClass
{
    public Guid ID {get;set;}
    public string Name {get;set;}
}
Malcolm O'Hare
  • 4,879
  • 3
  • 33
  • 53
  • Can't you consider `MajorClassID` simply as a scalar property (like `Name`)? Without the navigation property you basically don't have a relationship at all on the "O" side of the ORM. – Slauma Dec 10 '11 at 00:54
  • The problem behind this question is that EF is saving items in the wrong order becuase it is not aware of the relationships. All I really want to do is tell EF in what order to save items. – Malcolm O'Hare Dec 10 '11 at 04:25

1 Answers1

5

Navigation property is primary construct whereas foreign key is helper (imho wrong helper). EF recognizes ordering of DB commands by relationships which are defined by navigation properties. You cannot define relation just by foreign key. You need navigation property on at least one side of the relation.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670