Click here to Skip to main content
15,394,479 members
Articles / Programming Languages / C#
Posted 12 Oct 2014


7 bookmarked

Exclude Type from the Model in Entity Framework Code First

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
12 Oct 2014CPOL1 min read
Know how to exclude types from the Model in Entity Framework Code First

Points of Interest

In entity framework code first, the POCO (Plain Old CLR Object) classes are converted to tables. Entity framework looks for different areas of the application to collect all the classes which will be converted to tables in the database.

The followings are the different areas from where the tables are created if any type is found by Entity Framework Code First:

  1. The exposed DbSet<T> collections inside the context class
  2. If there is a reference from one type to other type that is mapped (i.e., the type is reachable from another type)
  3. If there is a reference of a type from any Fluent API method call on the DbModelBuilder

But this is not what we all need every time. Sometimes, we need to exclude some types from the model. We don't want to create tables for all the types which are related to the model in the application.

Entity framework has different options to prevent one type from being included in the model. The following are the ways which explicitly inform Code First to exclude a type:

  • Using [NotMapped] Data Annotation attribute
  • Using Ignore<T>() Fluent API method

Using [NotMapped] Data Annotation Attribute

Use [NotMapped] data annotation attribute to exclude a type from the model as follows:

public class DataTransferClass

Using Ignore<T>() Fluent API Method

Use Ignore<T>() Fluent API method to exclude a type from the model as follows:


Note: You need to include this inside the OnModelCreating() method. You cannot have this setting inside the EntityTypeConfiguration inherited classes.


  • 13th October, 2014: Initial version


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


About the Author

Dukhabandhu Sahoo
Software Developer (Senior)
India India
I have developed web applications using ASP.NET Web Forms, MVC, Web API, Entity Framework, SQL Server, LINQ, C#, jQuery, BootStrap UI and few other related libraries and tools.

Apart from programming, I love to watch cricket matches.

Comments and Discussions

-- There are no messages in this forum --