I have updated a project to use the new Entity Framework shipped with VS2012. However, I ran into some issues ’cause of which my code is throwing exceptions.
In the VS2010 version of Entity Framework, I was able to create or modify an Entity and use its navigation properties before calling SaveChanges()
For example:
A Navigation property of a client having a collection of Invoices.
Client c = new Client();
Invoice I = new Invoice();
c.Invoices.Add(I);
I would then be able to use Invoice.Client somewhere in my code before actually saving it without issue. Now Invoice.Client == null.
Similarly in setting I.Client = C, I does not show up under C.Invoices
What am I missing here or is this just simply how Entity Framework 5 is?
EDIT
In VS2010, I Created a new project, added the same DataSource and the following code worked as expected:
Client C = new Client();
C.Name = "Test";
Invoice I = new Invoice();
C.Invoices.Add(I);
MessageBox.Show(I.Client.Name);
Did the exact same thing in VS2012 and no dice.
In your above example you aren’t actually attached to the context.
Back-references for EF are only populated via a process called fixups which runs as a result of DetectChanges which in turn is triggered by (in your case) IDbSet<>.Add()
If you modify your code to attach Client to the context before adding an invoice to it you should see the expected behaviour
EDIT:
I haven’t really used this feature with DB First (.edmx) but you can enable it by using the below model type in 2010.
This model type has been removed in 2012 as STEs have been deprecated. (Although my understanding is that they still work for backward compatibility so if you were to upgrade a project from 2010-2012 you would still be able to use that model)