Entity Framework Application Using Code First

By | June 26, 2016

In this article I will show how we can work with Entity Framework. I will develop a WPF client application which will use code first technique. I have used WPF application for demo purpose only. The Client can be any application like asp.net, asp.net MVC. My application can be used as an code example to start working with entity framework in C#.

Become an Expert in Programming and Web Development.View all courses!

I have used Visual Studio Community 2015 for this article.

Entity framework allows us to access a relational database using strongly typed C# code or any .NET code. While we are working with the entity framework we don’t have to worry about the SQL connections, SQL commands, SQL parameters, SQL data readers and none of the low level abstractions we have programmed with in the past with .NET.

There are different ways to get started with entity framework.

  1. Schema First – Point to the existing database and it can import the database schema. It generates all the classes you need to query and update the database.
  2. Model First – In graphical designer we have the conceptual model for my application. It contains what classes do I want. Entity framework generates both the class definition and database schema.
  3. Code First – We have class definitions and entity framework uses these classes to create database.

Please follow these steps to start working with code First technique.

Entity Framework Code First Example

Step 1: Create a new WPF project. Install Entity framework by Going to Tools > NuGet Package Manager > Package Manager Console. Type Install-Package EntityFramework and Enter.

Step 2: Entity framework is installed.Create the project structure as shown below figure.

Entity Framework

Step 3 :The code for the Employee.cs and EmployeeDb.cs is as shown below.

 

As we can see from the above code both classed have Id field. The EF will implicitly create primary fields in these two tables using these fields. EF will also create foreign key reference between these two tables using EmployeeID foreign key ID.

Step 4: Next we will create the class which will help us in creating the tables and relationships in the data source. The class I have named is EmployeeDb. It will create the DB with same name. The code for the class is as shown below.

We have to use the name space using System.Data.Entity in this class. As seen in the above code there are two DbSet for Employee and EmplyeeReview.

Since these are the two properties which are of model type in our project. The entity framework will create two tables named same as these two properties in data source.

In the above code I am calling the overridden constructor of the DbContext base class where I can provide name of the connection string.

Step 5 : We have to create the View Model class named EmployeeViewModel.cs. The code for the class is as shown below.

One thing we should note here is that is MsSqlServer is also installed on the  machine, then the new DB will be created in the SQl Server Express by default if I do not provide the DefaultConnection connection string.

Here I have Set the DataDirectory to provide the path for creation of the EmployeeDb in localDB.

LocalDb is lighter version of the SQL DB which is installed with the Visual Studio. If we are working with data applications we can use localDB to work with out installing SQL server.

In the above code as soon as the instance of the EmployeeDb class is created a new Db in localDb will be created. The connection string in the App.config should be as shown below.

Now run the application. Whenever the application is executed the first time database and tables are created in the localDB.

Step 6: To see the data in the application change the code of the MainWindow.xaml.cs and MainWindow.xaml as shown below.

Step 7:To see the newly created Db in localDB. Click on View > Server Explorer. Click on Connect To DataBase icon (Top Green Color Plug Symbol). Select Data source as Microsoft SQL Server and Server Name as (LocalDB)\MSSQLLocalDB. Then select EmployeeDb from the Drop down. Click Ok. Check the figure below.

Entity Framework Code first

As we can see in the below figure two tables are created in the DB, named Employees and EmployeeReviews. Enter some data in Employees table. These were the two classes which we needed the Tables from in Step 4.

Entity Framework

 

Add data in this table. When you run the application you can see that data is populated in the Grid.

In my next article I will discuss about the migrations in entity framework.