Sunday, June 08, 2008

Entity framework and OpenFileDialog

Bugs manifest themselve in a strangest possible ways. Here is one of those...

1. Created a Winform App
2. One one of the forms in my app invoked window's OpenFileDialog
3. After closing the OpenFileDialog (not cancelling), any call to create new ObjectContext fails with following error:

The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.

Strange right ? How on earth can OpenFileDialog affect the creation of ObjectContext ?
After some close inspection and frustration, it turns out that the error message is actually pointitng to a correct cause:

The specified metadata path is not valid

Reason and Solution:

1. I have not decompiled or looked at the Entity framework code but at this point I am certian that the default constructor (the one which look at the connection string) of ObjectContext is trying to load the metadata from Environment.CurrentDirectory.
2. When application is started the Environment.CurrentDirectory = Application.StartupPath, therefore everything is happy and functional.
3. After closing the OpenFileDialog the Environment.CurrentDirectory property changes to wherever the dialog was pointing to.
4. But the connectionstring in appconfig is still point to "./*" which is transalting to Environment.CurrentDirectory, thus causing the load error.


Either change the autogenerated connection string to point to an absolute path.
Anytime you change the Environment.CurrentDirectory property, remember to change it back to Environment.CurrentDirectory = Application.StartupPath
Wait for the next release, hoping that the bug will be fixed after somebody reads this post ;-)

If this post does not help you then there is another onehere which explains the solution for the same error message but under totally different and unrelated circumstances.

1 comment:

Anonymous said...

Hi, I have somebody reporting this error for an application I made, but I can't seem to reproduce it. I work on a vista machine.. the person having this error is on XP. So I assumed (maybe I am wrong..)that somehow this isn't a problem on vista. I have added the fix anyway and will send an update, but before doing this I'd like to know if you think this has anything to do with XP?