Database Reference
In-Depth Information
Chapter 11
Functions
Functions provide a power mechanism for code reuse, and offer you a good way to make your code cleaner and
more understandable. They can also be used to leverage code in the Entity Framework runtime as well as in the
database layer. Functions are of various types: Rowset Functions, Aggregate Functions, Ranking Functions, and
Scalar Functions. Functions are either deterministic or nondeterministic. Functions are deterministic when they
always return the same result any time that they are called by using a specific set of input values. Functions are
nondeterministic when they could return different results every time they are called, even with the same specific set of
input values.
In the first seven recipes, we explore model-defined functions . These functions allow you to create functions at the
conceptual layer. These functions are defined in terms of Entity Framework types and your model entities. This makes
them portable across data store implementations.
In the remaining recipes, we show you how to use functions defined by Entity Framework and the database layer.
These functions are implemented for you, and they allow you to leverage existing code either in Entity Framework's
runtime or, closer to your data, in the database layer.
11-1. Returning a Scalar Value from a Model-Defined Function
Problem
You want to define a function in the conceptual model that takes an instance of an entity and returns a scalar value.
Solution
Suppose that you have a model like the one shown in Figure 11-1 .
Figure 11-1. A model for products and categories
 
Search WWH ::




Custom Search