Vince Plaza's Blog

...Microsoft .NET programming ideas and samples

August, 2009

...now browsing by month

 

Use a view to encapsulate business logic and reduce code

Friday, August 28th, 2009

This post adds to a previous post:Working with additive values in tables with different levels of detail

In the last post, we used a subquery to only show the latest value in a table that has a time component. The table holds an employee’s pay rate history and includes a “RateChangeDate” field. Although our use of a subquery makes sense, over time it will become tedious to type the same code every time we want to show an employee’s current pay rate. In addition, the use of the subquery adds risk to the quality of our application. The business logic represented by the subquery will need to be tested every place the subquery appears, increasing the odds for an error down the road.

Since we know the subquery is a handy block of code, we will take advantage of a common database feature called a “view”. In a nutshell, a view is a persistent select query that is given a name and can be referenced just like a table. With a view, business logic can be defined once and shared between future queries and even between programmers.

Taking our last example, lets turn the subquery for the latest pay rate and turn it into a view. Note how we use the “create” statement just like when making a table.
create view HumanResources.CurrentEmployeePayrateView
as
select    e.EmployeeId
          ,eph.Rate as Payrate
from      (
               select   EmployeeId
                        ,max(RateChangeDate) as RateChangeDate
               from     HumanResources.EmployeePayHistory
               group by EmployeeId
           ) e
inner join HumanResources.EmployeePayHistory eph on e.EmployeeId = eph.EmployeeId
                                    and e.RateChangeDate = eph.RateChangeDate

Click to continue »

Working with additive values in tables with different levels of detail

Tuesday, August 25th, 2009

Downloads: Sample Database

There are times when you need to perform a calculation on values stored in two different tables. The first value is stored in a summary level table, and the second is stored in a detail table. I will use the Adventure Works employee tables to demonstrate.

Let’s say you need to calculate the accrued vacation time for your employees that exceeds the company limit of 120 hours and derive the total liability to the company in terms of dollars. Using the Adventure Works database, that would require three tables: HumanResources.Employee, Person.Contact, and HumanResources.EmployeePayHistory. To start, we know we need to identify the employee so we include the EmployeeId field from the Employee table and the LastName and FirstName fields from the Contact table. Make sure and include the schema when declaring the tables, as this is required for any schema that is not ‘dbo’. This is a good practice anyway. Also, note the use of the table aliases ‘e’ and ‘c’ to save typing and add clarity.

select      e.EmployeeId
            ,c.LastName + ', ' + c.FirstName as EmployeeName
from        HumanResources.Employee e
inner join  Person.Contact c on e.ContactId = c.ContactId

Click to continue »

Update EF Model Properties to Match Conceptual Model

Thursday, August 13th, 2009

Before reading this post, read the previous post: Create an Adventure Works Entity Framework Model

Now that you have created an Entity Framework model for the Adventure Works database, you can begin to update properties to align the model with your business. In this example, I will change a few simple table properties to support future samples that use the model.

To start, open the AdventureWorks.edmx file in the Visual Studio GUI design tool. Select the SalesOrderDetail table by clicking on the table just below the table name in the header. The properties pane should look like this:

(click to enlarge)

(click to enlarge)


Click to continue »

Create an Adventure Works Entity Framework Model

Tuesday, August 11th, 2009

It is easy to create a simple Entity Framework model using the Adventure Works SQL Server database. I will use a WPF Model View project as my container. To start, create a new WPF Model View Application as described in this posting:

Right click on the Model folder in the Solution Explorer, select Add and then select New Item from the context menu.

(click to enlarge)

(click to enlarge)

Select the ADO.Net Entity Data Model template and name it AdventureWorks.edmx.

(click to enlarge)

(click to enlarge)

Click to continue »

Resetting metadata references in Entity Framework connection strings

Sunday, August 9th, 2009

I needed to move a couple of Entity Framework edmx files into a new project directory. Although I used the “add existing item” functionality of VS 2008, I still managed to corrupt the connection string entries in the App.config file, likely by using a subfolder. This led to a string of the following error message when I tried to run the app: “Unable to load the specified metadata resource.” After searching for a fix, I found that by toggling a property of the edmx file, it is possible to realign the connection to the new project.

To do this, open the model in the Visual Studio GUI tool for editing edmx files. Use your cursor to select any point in the background of the diagram where there is not an object. If you look at your properties pane, you will notice a property for the diagram called “Metadata Artifact Processing”.

Screenshot of VS2008 designer and properties pane

Screenshot of VS2008 designer and properties pane

 

The default value for this property is “Embed in Output Assembly” and this appears to be accepted as the best default practice. To repair your metadata reference, though, change the value to “Copy to Output Directory” and build the project. Now set the value back to the original “Embed in Output Assembly”.  Build the project again and you should be set to go. As this updates the App.config file, don’t forget to copy the updated entries to other projects that rely on the setting at run time.