Question

I'm developing an application using the EF w/ Self Tracking Entities. Because STE don't support Lazy Loading, what is the preferred method or best practice for development of updating navigation properties of an entity after making changes to the foreign key? This is explained here:

http://msdn.microsoft.com/en-us/library/ff407090.aspx

"When you change the relationship between objects by setting the foreign key property, the reference navigation property is set to null and not synchronized to the appropriate principal entity on the client. After the graph is attached to the object context (for example, after you call the ApplyChanges method), the foreign key properties and navigation properties are synchronized."

This issue has been addressed here:

http://blogs.msdn.com/b/diego/archive/2010/10/06/self-tracking-entities-applychanges-and-duplicate-entities.aspx

But is this really the best way to do this? What are some ways that you guys have used or can think of to get around this limitation? I find it hard to belive anyone is using self tracking entities if its this hard to display navigation properties in you datagrids and have them set to null whenever you make a change to the record. It looks funky in my application when this happens. Sure I can go back to the server after a save and update these properties but I would have a bunch of null navigation properties in my datagird until I do a call to save on the server.

For example, I have a master details scenario in which my user select a record form the datagrid, then edits it in the form below. When changing the foreign key that my combobox is bound to, the record bound to the navigation property in the datagrid gets set to null. How can I avoid this behavior and update the navigation property when the foreign key is changed?

If I try to change the relationship by setting the navigation property instead of the foreign key, i get the following exceptions when I try to submit changes:

"AcceptChanges cannot continue beacuse the objects key values conflict with another object in the ObjectStateManager. Make sure the key values are unique before calling AcceptChanges."

Is it not possible to bind a master details datagrid to navigation properties of entities? Any help or direction on overcoming this would be greatly appreciated.

Thanks!

Was it helpful?

Solution

A little bit frustrating that there seems to be very little information about these types of problems. In the end I was able to figure it out by doing the #2 suggestion from this blog post and grouping my service call to one method:

http://blogs.msdn.com/b/diego/archive/2010/10/06/self-tracking-entities-applychanges-and-duplicate-entities.aspx

The problem tho is that if I want to go back to the database and re load my data I will have to also re-load any lookup tables so they come back in the same graph to avoid the error. I hope this changes in future version of STE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top