Wednesday, July 27, 2011

Using SQLite with Entity Framework in Visual Studio 2010

Introduction

In my previews blog post we have seen how using a simple “data provider” provided by http://sqlite.phxsoftware.com/ can make our life easier in context of SQLite in .net. In this short article we are going to see how we can use SQLite to generate Entity Framework Data model.

Choosing the Provider

First thing first create any type of project in Visual studio 2010. But I would prefer an web application or windows forms or perhaps a WPF application. Now click on project and select add new item to add a new item type. From Add new item wizard select “Ado.net Entity Data Model” and click on add this will brought up the Entity data model wizard.

  • Step 1: Select Generate from Database
  • Step 2: In next window select New connection
  • Step 3: Connection properties window will popup and the select right kind of provider for SQLite.
  • Step 4: After that the Connection properties will have browser or New db,

Create Data Model

  • Step 5: Now select existing db if you have using browser or New to create new db.
  • Step 6: Continue with the wizard to select database table items to include in model.
  • Step 7: Click finish to close the wizard and a data model will be generated for you.

Data Model Created

If you had any relationship in the database those will be automatically be synchronized in data model, that’s it we are good to jump in the code to retrieve data and do all type of necessary stuff that we must do in a database. Best of luck and happy coding.

3 comments:

walter said...

How do u embed the database. e.g. what to have a wpf application with database in the application folder

Munna said...

Add the database in App_Data or some convenient folder in your project as content.
Use the bellow code to get FQN Path and use this in db connection.

String dbFileURL = System.IO.Path.Combine(Assembly.GetEntryAssembly().Location, @"App_Data\BankingDB.mdf");

gokhaled89 said...

Can you give me an example as to how to make the path relative? I tried that but while entity framework generates the data source property it takes the path "C:\sqlite\....", it should come something like "@App_Data\PersonalExpenseDB.s3db".
Please help..

Post a Comment