Entity Framework – Database First Approach Example


In this article, Let’s see an example of Entity Framework – Database First approach

If you don’t know about Entity Framework, please go through this article.

Database First allows you to reverse engineer a model from an existing database. The model is stored in an EDMX file (.edmx extension) and can be viewed and edited in the Entity Framework Designer. The poco classes are automatically generated from the EDMX file.

Entity Framework – Database First Approach Example

1. Let’s create a console application. Name it as “EFDatabaseFirstExample”.

2. We will create a model by using Entity Framework Designer for our existing database. Let’s add a new item to our project “ADO.NET Entity Data Model”.

3. It will start a wizard, now as we have existing database, we will select “Generate from Database”.

4. Choose the connection string, pick a connection to our existing database and click on “Next” button.

5. Wizard will ask you to pick the Entity Framework version to use. Just select the latest version and click “Next”.

6. Now choose the database objects like tables, views, stored procedures etc from the screen.

7. Model is created now and you can see the entities.

8. During the process when you select Entity Framework version, the reference to Entity Framework Assembly is automatically added.

So after step 7, you will see following things in the project.

9. Since everything is in place, let’s code to add a employee for a company and display all the employees for a company.


Console.WriteLine("Please enter employee name");
using (var db = new EFDatabaseFirstExample.EFDatabaseFirstDBEntities())
{
Employee employee = new Employee();
employee.Name = Console.ReadLine();
employee.CompanyId = 1; //hard coded for demo purpose
employee.Age = 27;
db.Employees.Add(employee);
db.SaveChanges();
}

Console.WriteLine("List of employees for company 1");
using (var db = new EFDatabaseFirstExample.EFDatabaseFirstDBEntities())
{
//get the company with ID = 1
var company = (from c in db.Companies
where c.Id == 1
select c).SingleOrDefault();

//get the employee list for a company with a id = 1
List<Employee> employees = company.Employees.ToList();

//display each item.
foreach (var emp in employees)
{
Console.WriteLine(emp.Name);
}
Console.ReadKey();
}

Hope this helps. Please comment below the post if you have any doubts or questions.

Source Code Download:

Github [Repository Link]

Box.com [Direct Download Link]

Advertisements

3 thoughts on “Entity Framework – Database First Approach Example

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