So we’ve been doing some research (and training) in Microsoft’s Entity Framework, which has been very riveting. We even had the assistance of a bonafide EF guru for one day, Julie Lerman from The Data Farm, and after her presentation on how to set up an Entity Framework and use it I was all hot and bothered to do one of my very own.
The Project
I wanted to create a simple WinForms application to allow Tasha to catalog her ever-growing library, and I wanted to do it using RESTful Web Services and a small database including Entity Framework. I found some great web services to use for looking up the books (ISBNdb.com) and cover art (LibraryThing.com). Awesome. We’re talking about roughly two tables here, one for book data and one for the cover art. Should be easy! I got my developer keys signed up and proceeded to play around with the XML services that ISBNdb offers. Looks like my needs will be met!
The Problems
I opted to use a local SQL Server CE database, so I created a very simple Books table. I go through the steps so excellently outlined by Julie, and BAM I’ve got myself an Entity Framework that allows me to do some pretty neat LINQ to Entities style querying. Microsoft is even so good as to generate you a quickie database connection for your local database. I check the table I created so I can see my row, but much to my dismay it does not appear! I debug… the Entity Framework seems to think it is manipulating a row correctly and everything is happy.
After MANY attempts at fixing this problem, I grow desperate and contact Julie for help. She walks me through some debugging and troubleshooting steps and I still ended up with issues. It’s not her job to help me debug my toy programs, so I soldier on to find solutions on my own. I did some searches about this very problem and came up dry for most of the weekend, until about 15 minutes ago.
The Solution
It seems that Microsoft has created an attribute called |DataDirectory| to point at the location of the SQL Server CE database. The trouble is that it (and the auto-generated database connection) defaults to the instance of the file that is created in your project’s root directory. When you are running your app in Debug mode, the app is actually acting on the COPY of the database in your /Debug folder.
Thanks to Tim Anderson’s ITWriting Blog, I was able to identify this problem and figure out how to reset my DataDirectory to the currently executing assembly directory. This is probably unnecessary, as I think the system is doing this for me but I have found in situations like this it is better to explicitly set a dynamic directory to a location you know instead of the location that the goblins that work for Microsoft choose.
After knowing better, I changed my auto-generated database connection to the version located in my Debug directory (where I will be doing most of my coding) and sure enough… there was the record I was looking for all that time.
In Conclusion
Chalk this one up to ignorance, I guess. I’m going to get underway with hooking up to the web services (which I am experienced with) and building up the WinForms app (the fun part) and I’ll revisit the database once I have real data to insert into it! Thanks to Julie for being so helpful and Tim for having the same problem and blogging about the solution.
Hooray for continuous improvement!!
Related Articles
2 users responded in this post