February 14, 2011

Entity Framework with Windows Form Application and Microsoft SQL CE

This is a fastest tutorial for learning about working Entity Framework with SQL Server Compact Edition and Windows Form Application. I promise to you that is a simplest and overall :-D


1- Open Microsoft Visual Studio 2010
2- Add New Project from menu with select File > New > Project > Windows Form Application from New Project window

3- Go to Solution Explorer and right-click on solution and select Add > New Project > Class Library from New Project window

4- Go to Solution Explorer and right-click on ClassLibrary1 and select Add > New Item > Local Database from Add New Item window (Cancel the Data Source Configuration Wizard window which opend after that)
5- Go to Solution Explorer and double-click on Database1.sdf
6- Go to Server Explorer and right-click on Tables (from Database1.sdf) and select Create Table from context menu
7- Add a table and set TEST for name it like below screen shot.
8- Go to Server Explorer and open Tables and right-click on TEST table and select Show Table Data from context menu and add some data in rows.

9- Go to Solution Explorer and right-click on ClassLibrary1 and select Add > New Item > ADO.NET Entity Data Model > Generate from database > Next > select TEST from Tables > Finish
10- Go to Solution Explorer and right-click on References form WindowsFormApplication1 > Add Reference > Projects > ClassLibrary1

11- Press F6 for Build Solution and TAKE A BREATH :-D
12- Select WindowsFormApplication1 from Solution Explorer and go to menu and Data > Add New Data Sources > Object > ClassLibrary1 > ClassLibarary1 > TEST
13- Double click on Form1.cs from WindowsFormApplication1 in Solution Explorer
14- Go to menu and select Data > Show Data Source and select TEST and drag and drop it to Form1

14.1- If did you have an error about with below title just go to Solution Explorer > WindowsFormApplication1References > Add References > .NET > System.Data.Entity and double-click on it.

Error 1 The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

15- Double click on a empty space of form and go to Form1.cs > Form1_Load method and add below line for fetch data from database
this.tESTsBindingSource.DataSource = new ClassLibrary1.Database1Entities().TESTs;

16- ENJOY :-D

PS: Hey folks, if did you have any problem please don't be hesitate to contact me or write comment here ;-)

Entity Framework Tutorials
Entity Framework with Windows Form Application and SQL CE

Source Codes: link


Anonymous said...

how i can add element from bindingsource

Anonymous said...


That is a very simple scenario pulling a list of all data and showing that in a grid.

What if i need to do a proper SELECT, INSERT or UPDATE such as :
Select * from test where ID in (1,2,3) ???

Any ideas

Please email me lennybacco@gmail.com

Anonymous said...

Where are the layers? and seperation of concern?