Beginners Tutorial on Entity Framework : A Console Application

Let us start with the Introduction to Entity Framework. Entity Framework is one of the ADO.NET approaches, which is used for communication between the application and the database. It is a Open Source project.

Entity framework is widely used with Microsoft .NET applications, that provides database connectivity using a model class (whose object is often referred as context) that forms the abstraction of database objects as classes and properties. There are two approaches, viz Creating model from database and creating database from models. Advantage of entity framework is, it reduces the dependency of database admin and makes the most of the code to handle database operations using the Model Designer. Entity framework often make use of LINQ to query the database objects. Care should be taken while using LINQ which may affect the performance, in case of the use of improper LINQs (Language INtegrated Query) .

Now let us see how to create a console application using Entity Framework.

Steps to create the Model from Database:

  1. Create a new console application.
  2. Right click the project and click “add new item” >> Select “Data” from the left side of the wizard >> Select “ADO.NET Entity Model” >> Name your Entity Model >> click “Add”.
  3. A “Entity Data Model Wizard” will appear.
  • Select “Generate from Database” >> Click “Next”
  • Create the connection to the database. Click “Next”.
  • Select “Tables” >> Click “Finish”

Note: The steps are illustrated in below. Follow the presentation for more clarification.

Step 1:  Create a new Console Application

Step 2: Add a item “ADO.NET Entity Model to the Project”

Step 3: Run the “Entity Data Model Wizard”

After running the wizard go to Program.cs in your console application.
Follow the steps to create, delete and display the database records, as demonstrated below
1 )Add the required .dlls to the header

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.Objects;

using System.Data;

2) The code to insert record into db

In the Main method of program.cs add the following code.

using (SampleDatabaseEntities context = new SampleDatabaseEntities())

{

Console.WriteLine(“Enter a Country Name”);

string country = Console.ReadLine(); //read the country name to be stored

Country con = new Country(); //Create an object to the Country Entity

con.Name = country;

context.AddToCountries(con); //Equivalent to inset query

context.SaveChanges();

}

3) The Code to display the records from the database

using (SampleDatabaseEntities context = new SampleDatabaseEntities())

{

int i = 1;

var CountryTable = from offices in context.Offices

join countries in context.Countries on offices.CountryId equals countries.Id

select new

{

CountryName = countries.Name,

Location = offices.Name

};

foreach (var ent in CountryTable.ToList())

{

Console.WriteLine(i + “\t” + ent.CountryName + “—–” + ent.Location);

i++;

}

}

4) The Code to delete a record from database

using (SampleDatabaseEntities context = new SampleDatabaseEntities())

{

int i=1;

var locations = from loc in context.Offices

select loc.Name;

Console.WriteLine(“Enter the Location Name to be deleted from the list”);

foreach (string loc in locations)

{

Console.WriteLine(i+”)”+loc);

i++;

}

string locationDel = Console.ReadLine().ToUpper();

Office record = (from loc in context.Offices

where loc.Name == locationDel.ToUpper()

select loc).FirstOrDefault();   //Fetch the object to be deleted

context.DeleteObject(record); // the object to be deleted

context.SaveChanges();

}

Happy Learning!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s