Sqlite & Entity Framework 4
- by Dane Morgridge
I have been working on a few client app projects in my spare time that need to persist small amounts of data and have been looking for an easy to use embedded database. I really like db4o but I'm not wanting to open source this particular project so it was not an option. Then I remembered that there was an ADO.NET provider for sqlite. Being a fan of sqlite in general, I downloaded it and gave it an install. The installer added tooling support for both Visual Studio 2008 & 2010 which is nice because I am working almost exclusively in 2010 at the moment. I noticed that the provider also had support for Entity Framework, but not specifically v4. I created a database using the tools that get installed with Visual Studio and all seemed to work fine. I went on to create an Entity Framework context and selected the sqlite database and to my surprise it worked with out any problems. The model showed up just like it would for any database and so I started to write a little code to test and then.. BAM!.. Exception. "Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information." A quick bit of searching on Bing found the answer. To get it working, you need to include the following code in your web.config file: 1: <startup useLegacyV2RuntimeActivationPolicy="true">
2: <supportedRuntime version="v4.0" />
3: </startup>
And then everything magically works.
Entity Framework 4 features worked, like lazy loading and even the POCO templates worked. The only thing that didn't work was the model first development. The SQL generated was for SQL Server and of course wouldn't run on sqlite without some modifications. The only other oddity I found was that in order to have an auto incrementing id, you have to use the full integer data type for sqlite; a regular int won't do the trick. This translates to an Int64, or a long when working with it in Entity Framework. Not a big deal, but something you need to be aware of.
All in all, I am quite impressed with the Entity Framework support I found with sqlite. I wasn't really expecting much at all, and I was pleasantly surprised.
I downloaded the ADO.NET sqlite provider from http://sqlite.phxsoftware.com/. If you want to use an embedded database with Entity Framework, give it a look. It will be well worth your time.