DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Conditional Mapping in Entity Framework

Using Conditional Mapping in Entity Framework

Gil Fink user avatar by
Gil Fink
·
Jun. 21, 10 · News
Like (1)
Save
Tweet
Share
8.79K Views

Join the DZone community and get the full member experience.

Join For Free
There are times that 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 use 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>:



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 mapping 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 check whether a column is Null or Not Null.  When we have more then 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 indicate a logical delete (for example an IsDeleted field). Since we want to present only undeleted rows/entities then a conditional mapping can be a valid solution.

In the example I’m using the following table that represent a course data:



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:



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 retrieve only entities with IsDeleted equals to 0 in the following way:



That is it.

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

SELECT 

[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 then there is a casting in the query.

Summary

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.

Database Entity Framework Framework

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Best Cloud Provider for Hosting DevOps Tools
  • Using JSON Web Encryption (JWE)
  • Type Variance in Java and Kotlin
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: