Book Image

Mastering Entity Framework

By : Rahul Rajat Singh
Book Image

Mastering Entity Framework

By: Rahul Rajat Singh

Overview of this book

<p>Data access is an integral part of any software application. Entity Framework provides a model-based system that makes data access effortless for developers by freeing you from writing similar data access code for all of your domain models.</p> <p>Mastering Entity Framework provides you with a range of options when developing a data-oriented application. You’ll get started by managing the database relationships as Entity relationships and perform domain modeling using Entity Framework. You will then explore how you can reuse data access layer code such as stored procedures and table-valued functions, and perform various typical activities such as validations and error handling. You’ll learn how to retrieve data by querying the Entity Data Model and understand how to use LINQ to Entities and Entity SQL to query the Entity Data Model.</p>
Table of Contents (19 chapters)
Mastering Entity Framework
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using Entity Framework with functions


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.

Using scalar functions

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...