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 http://sqlite.phxsoftware.com/ or the direct link http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/1.0.66.0/SQLite-1.0.66.0-setup.exe/download .

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:


<connectionStrings>
  <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"'" 
providerName="System.Data.EntityClient"   
connectionStrings>;

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;






References: http://dotnet.dzone.com/news/sqlite-entity-framework-4

No comments:

Post a Comment