Database Reference
In-Depth Information
We start by clearing out any previous test data in the underlying data store. Notice how we wrap the
Cleanup() operation inside an async method. We then generate native SQL statements using the new
ExecuteSqlCommandAsync() method. Note how we leverage the async/await patterns found in the 4.5 version of the
.NET framework. This pattern enables asynchronous operations without explicitly instantiating a background thread;
additionally, it frees up the current CLR thread while it is waiting for the database operation to complete.
Next we load test data for both Associate and Associate Salaries. To execute the call asynchronously, as before,
we wrap the LoadData() operation inside an async method and insert new test data into the underlying data store by
calling the newly added SaveChangesAsync() method.
Next, we present three different queries that go against the model. Each leverages the LINQ extensions
for Entity Framework. Each is contained within an async method, leveraging the await/async pattern. In the
RunForEachAsyncExample() method, we make use of the ForEachAsync() extension method, as there is no async
equivalent of a foreach statement. Leveraging this async method, along with the Include() method, we are able to
query and enumerate these objects asynchronously.
In the subsequent RunToListAsyncExample() and RunSingelOrDefaultAsyncExample() queries, we leverage the
new asynchronous methods for ToList() and SingleOrDefault() .
Entity Framework now asynchronously exposes a large number of its operational methods. The naming
convention appends the suffix Async to the existing API name, making it relatively simple to implement asynchronous
processing when adding or fetching data from the underlying data store.
3-2. Updating with Native SQL Statements
Problem
You want to execute a native SQL statement against the Entity Framework to update the underlying data store.
Solution
Let's say that you have a Payment database table like the one shown in Figure 3-2 , and you have created a model such
as the one in Figure 3-3 , which is from the Entity Framework designer tool.
Figure 3-2. A Payment table that contains information about a payment made by a vendor
Figure 3-3. A model with a Payment entity
 
Search WWH ::




Custom Search