Book Image

WCF Multi-layer Services Development with Entity Framework - Fourth Edition

By : Mike Liu
Book Image

WCF Multi-layer Services Development with Entity Framework - Fourth Edition

By: Mike Liu

Overview of this book

Table of Contents (20 chapters)
WCF Multi-layer Services Development with Entity Framework Fourth Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Hosting the HelloWorld WCF Service
Index

Querying and updating a database table


Now that we have the entity classes created, we will use them to interact with the database. We will first work with the Products table to query and update records as well as to insert and delete records.

We will put our code in the Program.cs file. To make it easier to maintain, we will create a method, TestTables, put the code inside this method, and then call this method from the Main method.

Querying records

First, we will query the database to get some products. To query a database by using LINQ to Entities, we first need to construct a DbContext object as follows:

var NWEntities = new NorthwindEntities();

We can then use the LINQ query syntax to retrieve records from the database using the following code:

IEnumerable<Product> beverages = from p in NWEntities.Products
                     where p.Category.CategoryName == "Beverages"
                     orderby p.ProductName
                     select p;

The preceding code will retrieve all of...