Sunday, February 20, 2011

Sqlite plus Entity Framework 4

I have just started working with Sqlite, and i decided that it would be very interesting to use it together with Entity Framework. So lets just see how to do it:

The first step is to download Sqlite from or the direct link .

This will install the provider to use with entity framework.

Then we have to had a entity model to our project:

And it is done, now we just have to use it:

using (var dataContext = new OurEntities())
   var statusList = from status in dataContext.Status
                        select status;
   foreach (var status in statusList)
      Console.WriteLine(status.Name + " - " + status.Description);

By the way, do not forget to add the connection string to your web.config or app.config:

  <add name="OurEntities" 
connectionString="metadata=res://*/OurEntityModel.csdl|res://*/OurEntityModel.ssdl|res://*/OurEntityModel.msl;provider=System.Data.SQLite;provider connection string='data source=".\Database\OurDatabase.db"'" 

Notes: If you are using the version 4 of the framework you must add the following line to your web.config or app.config:

<startup useLegacyV2RuntimeActivationPolicy="true">;
    <supportedRuntime version="v4.0" startup;


No comments:

Post a Comment