Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Windows Azure – SQL Azure – Using ADO.NET Entity Framework – Data Operations

Posted by Paul on November 13, 2011


In this article we are going to explore data operations using ADO.NET Entity Framework over our SQL Azure database. The data model creation which is an essential part required was discussed in the previous article.

Pre-Requisites

For working on this application, you need to have the following pre-requisites.

  • Azure project with web role
  • Entity data model with Employee class

You may use the project from the previous article.

After having the above, place the following on the Default.aspx page

  • 4 buttons with labels Insert, Update, Delete, Refresh
  • A grid control

clip_image002

Insert Operation

For creating a new employee, use the following code in the Insert button click event.

mysqlazureEntities context = new mysqlazureEntities();

Employee employe = new Employee();

employe.Name = “New Employee from EF”;

context.AddToEmployees(employe);

context.SaveChanges();

Update Operation

For creating a new employee, use the following code in the Update button click event.

mysqlazureEntities context = new mysqlazureEntities();

Employee employee = context.Employees.Where(emp => emp.Name == “New Employee from EF”).FirstOrDefault();

if (employee != null)

employee.Name = “Updated Name”;

context.SaveChanges();

Delete Operation

For creating a new employee, use the following code in the Delete button click event.

mysqlazureEntities context = new mysqlazureEntities();

Employee employee = context.Employees.Where(emp => (emp.Name == “New Employee from EF” || emp.Name==”Updated Name”)).FirstOrDefault();

if (employee != null)

context.DeleteObject(employee);

context.SaveChanges();

Select Operation

For creating a new employee, use the following code in the Refresh button click event.

GridView grid = GridView1;

grid.Columns.Clear();

grid.Columns.Add(new BoundField() { HeaderText = “Id”, DataField = “Id” });

grid.Columns.Add(new BoundField() { HeaderText = “Name”, DataField = “Name” });

mysqlazureEntities context = new mysqlazureEntities();

grid.DataSource = context.Employees.ToList();

grid.DataBind();

On executing the application and playing with the buttons you can view the following results.

clip_image004

Summary

In this article we have explored the data operations using ADO.NET Entity Framework over our SQL Azure database. You need to change the database name, user name and password in the configuration file.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s