I have an entity framework code first object with a self referencing key. The child records don't have the parent id in the self referencing key when the data is written to the database.
public class Contract
{
public Contract()
{
Contracts = new List<Contract>();
}
public int Id { get; set; }
public List<Contract> Contracts { get; set; }
}
The object has many more properties than above but I've simplified for the sake of example. This is not a many to many relationship. Each contract has only one parent, apart from master contracts that have no parents.
When I add a sub contract to the list of contracts and I call SaveChanges on the DBContext, the contract and sub contract are written to the database with all fields correct. Apart from the entity framework generated field Contract_Id which is null.
I've got similar self referencing keys working correctly with other classes, in fact the contract has these classes as properties. These other classes all work as expected, with the self referencing keys indicating the parent object all populated correctly.
I've created a very simple test class the same as above just with Test prepended to the class name and that works correctly. I've created a simple test with my Contract class and that does not work correctly.
What I'd like to be able to do is debug the entity framework SaveChanges method but I'm not sure that's possible.
Does anybody have any idea what to look for in terms of what I've done wrong or how to debug this?