Functions provide an excellent way of writing reusable logic in the data layer itself. Functions are computed values and cannot perform permanent environmental changes to the SQL Server. There are two types of functions that can be defined in the database:
Scalar functions
Table values functions
Let's see how we can use these functions using Entity Framework.
A scalar function is a function that is defined in the database and returns a scalar value. For example, if we need to find the EmployerId
for an Employee
by passing the ID
of the Employee
, we can do this by defining a scalar function as follows:
Create FUNCTION dbo.GetEmployerIdForEmployee (@id int) RETURNS int AS BEGIN DECLARE @result int; select @result = EmployerId from Employee where ID = @id; RETURN @result; END
Unfortunately, Entity Framework does not support using a scalar function directly. If we want to use a scalar function in our application, we have...