Click here to Skip to main content
13,043,898 members (55,116 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 18 Jul 2010

Using Conditional Mapping in Entity Framework

, 18 Jul 2010
Rate this:
Please Sign up or sign in to vote.
Using Conditional Mapping in Entity Framework when we want to retrieve only a portion of data from a table in the database permanently by some filter.


There are times when we want to retrieve only a portion of data from a table in the database permanently by some filter. For that purpose, we can use the conditional mapping in Entity Framework.

What is Conditional Mapping?

Conditional mapping is a fixed condition that helps us to filter the result set that is being returned from the database for a specific entity. Also it enforces that an entity is mapped to data in the database under only certain conditions which are supplied inside the conditional mapping. In order to use conditional mapping, we need to open the Mapping Details View. In the view, we can add for each entity a conditional mapping using the <Add a Condition>:

Conditional Mapping

When we create a condition, that condition will be added to each query that we will make to the database.

Available Conditional Mappings

There are some possible conditional mappings in Entity Framework which are provided by two kind of operators – the equality operator and the Is operator. The equality operator (=) can have values of strings or integers. The Is operator checks whether a column is Null or Not Null. When we have more than one condition, it will construct an And operation between all the conditions.

Conditional Mapping Example

A very common example for using a conditional mapping is having a table field that indicates a logical delete (for example, an IsDeleted field). Since we want to present only undeleted rows/entities, a conditional mapping can be a valid solution.

In the example, I'm using the following table that represents a course data:

Course Table

The IsDeleted has a default value of 0 to indicate that a row’s logical state is not deleted. If the row is deleted, I change the value to 1.

This is the entity in the generated model before I make any changes:

Course Entity

Now I want to impose the conditional mapping on the course entity. The first thing to do is to delete the IsDeleted property since it will be used by the conditional mapping. Then I create a conditional mapping that retrieves only entities with IsDeleted equals to 0 in
the following way:

The Conditional Mapping

That is it.
Now whenever I query for courses, the generated query will be created with a where clause that checks whether IsDeleted equals 0.
For example if I query for all courses, the following query will be sent to the database:

[Extent1].[CourseID] AS [CourseID], 
[Extent1].[Title] AS [Title], 
[Extent1].[Days] AS [Days], 
[Extent1].[Time] AS [Time], 
[Extent1].[Location] AS [Location], 
[Extent1].[Credits] AS [Credits], 
[Extent1].[DepartmentID] AS [DepartmentID]
FROM [dbo].[Course] AS [Extent1]
WHERE [Extent1].[IsDeleted] =  CAST( '0' AS tinyint)

Since I use tinyint as data type in the database, there is a casting in the query.


A conditional mapping in Entity Framework helps to filter returning results. If you have a fixed behavior such as logical delete, then conditional mapping can help you achieve the relevant behavior.

</img />
</img />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...


Comments and Discussions

QuestionCan't update soft delete column Pin
QStarin16-Aug-11 12:17
memberQStarin16-Aug-11 12:17 
AnswerRe: Can't update soft delete column Pin
Gil Fink16-Aug-11 21:27
memberGil Fink16-Aug-11 21:27 
QuestionHow to do this programmatically? Pin
dachcisiepali22-Nov-10 22:29
memberdachcisiepali22-Nov-10 22:29 
AnswerRe: How to do this programmatically? Pin
Gil Fink25-Nov-10 0:33
memberGil Fink25-Nov-10 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid