Create a master-detail Windows Forms application with EF
Dec 12, 2020 15:02 0 Comments Other RAMU

                                         Create a master-detail Windows Forms application with EF

In this post I will show you a step by step example on how to build a Windows Forms (Master – Details) application with C# and Entity Framework. Have a look in this post if you would like to see how to implement similar functionality with WPF.

First let’s try to define what EF is and why it is going to help us to create easily data-centric applications.Entity Framework is an object-relational mapping (ORM) framework for the .NET Framework.EF addresses the problem of Object-relational impedance mismatch. I will not be talking about that mismatch because it is well documented in many sites on the Internet. Through that framework we can program against a conceptual application model instead of programming directly against a relational schema-model. By doing so we can decrease the amount of code we do write to access a data storage and thus decrease maintenance time.

In my other development blog (ASP.Net blog) you can find many posts on Entity Framework.Have a look if you want.

We will need a database and a version of SQL Server.You can download and install the free SQL Server Express edition from here. If you need the installation scripts for the sample AdventureWorkLT database, click here

Let’s start with our hands-on example.

1) Launch Visual studio 2010 Express edition or any other edition you may have installed.

2) Create a new class library project and name it AWEFModel. Choose C# as your language of development.Ιn this project I will keep my Entities Data Model in a separate project and then I will consume this project from a separate application (Windows Forms)

3) Remove the Class1.cs file.Right – click on your project from the Solutions Explorer window and Add a new Item. From the available templates choose ADO.NET Entity data model. Give it the name ΑdventureWorksLTEntities.edmx and click the Add button.

4) Then the Wizard pops up. Choose “Generate from Database”. Have a look at the picture below

5) Click Next and Create a New Connection (as you see in the picture below) and hit the Continue button.

6) Then follow the instructions as shown in the picture below.Select the local installation of your SQL Server and the database name and then click OK.


7) After you complete the steps above you will see something like the picture below. You can see the Entity connection string and where these setting will be saved.Hit the Next button.Then click Finish

8) If you notice you will see that there is something called Entity connection string that points to the actual database.It is saved in the App.config file of the

9)  Now the wizard will identify the database objects and let us choose which database objects we want to include in our model.I included all the database objects.Hit the Finish button.Ι will include only some of the tables. Have a look at the picture below

Finally, Visual Studio will add the model to the project and will add the necessary references.

10) Have a look at your entity model as it is visualized in the Entity Model Designer. Have a look at the Entity classes and their relationships/associations.Have a look at the properties of each Entity class. Have also a look at the Navigation Properties of each entity class.Basically the entity framework generates classes for all the entities in the designer. You can have a look at the generated code if you want by opening the class file, which in my case is AdventureWorksLTEntities.Designer.cs.

11) Add a new project to your solution, a Windows Forms Application. Name it WindowsFormsEFMD. Add a reference to the System.Data.Entity  and the Entity Framework Model project (AWEFModel)

12) Copy paste the App.config file from the AWEFModel project to the WindowsFormsEFMD project.

13) We will add a datasource to the Windows Forms application (Data–> Add new Datasource). We will add a datasource for the Customer Entity. In the wizard that pops up (Choose a Data Source Type) select Object and then click Next.In the Select the Data Objects choose from the AEFModel(It is already in the there) the Customer class /entity. Change the Customer Entity from GridView to Details. Drag and drop the Customer Entity from the Data Sources to the Form1 window.

Have a look at the picture below to see what I mean

Drag and drop from the Data Sources (SalesOrdersHeaders) entity to the Form1 form.

Have a look at the picture below.

14) In the Form1_Load event handling routine type,

1

2

3

4

5

6

7

8

9

private void Form1_Load(object sender, EventArgs e)

{

var ctx = new AdventureWorksLTEntities();

 

var query = ctx.Customers.Where(c=>c.SalesOrderHeaders.Any());

 

customerBindingSource.DataSource = query.ToList();

 

}

15) Ι am querying the database to find only customers that have orders. Run your application.

16) I will show you how easy it is to make changes. Please note that all the changes are made to the entities in the memory and then are persisted back to the database.

17) Go to the customerBindingNavigator control in the Form1 design view.Select the “Save” button and enable it (right-click on it).

18) Then in the customerBindingNavigatorSaveItem_Click event handling routine type.

1

2

3

4

5

private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)

{

 

      ctx.SaveChanges();

    }

I have made a very small change in the code. I have instantiated the context outside of the local scope

1

2

3

4

5

6

7

public partial class Form1 : Form

{      AdventureWorksLTEntities ctx = new AdventureWorksLTEntities();

 

public Form1()

{

InitializeComponent();

}

19) The ctx.SaveChanges() method persists the changes to the database.Run your application and make some changes in the Customers Grid. Click the Save button to persist the changes back to the database. Launch SQL Profiler to see what actually happens in the database.

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect