Back to: Entity Framework Tutorials For Begineers and Professionals
NotMapped Attribute in Entity Framework
In this article, I am going to discuss NotMapped Data Annotation Attribute in Entity Framework Code First Approach with Examples. Please read our previous article where we discussed InverseProperty Attribute in Entity Framework Code First Approach with Examples.
NotMapped Attribute in Entity Framework
The NotMapped Attribute in Entity Framework can be applied to one or more properties of an entity class for which we do not want to create corresponding columns in the database table. By default, Entity Framework API creates a column for each property (property having the get and set accessors) in an entity class. Using this NotMapped Attribute, we can also exclude a class from database mapping i.e. if we apply this attribute on an Entity, for this Entity no database table is going to be created in the database. So, the [NotMapped] Attribute overrides the default convention which is followed by Entity Framework API.
Examples to Understand NotMapped Attribute in Entity Framework:
Before using the NotMapped Attribute in Entity Framework, let us first see the signature of this Attribute class. If you go to the definition of this class, then you will see the following. This Attribute denotes that a property or class should be excluded from database mapping. As you can see, this class has one parameterless constructor.
Let us understand the NotMapped Attribute with an Example. Please modify the Student.cs class file as shown below. Here, we are creating two Entities with the name StudentOnline and StudentOffline with the same set of properties. Then we applied NotMapped Attribute with RegistrationNumber Property of the StudentOnline Entity class and this should exclude the corresponding database column in the database while creating the StudentOnline database table. Then we applied the NotMapped attribute on the StudentOffline Entity class and this should exclude the corresponding database table to be created in the database.
using System; using System.ComponentModel.DataAnnotations.Schema; namespace EFCodeFirstDemo { public class StudentOnline { public int StudentOnlineId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } //Applying NotMapped Attribute in Properties //For the following Property, no database column will be created [NotMapped] public int RegistrationNumber { get; set; } } //Applying NotMapped Attribute in Entity Class //For the following Entity, no database table will be created [NotMapped] public class StudentOffline { public int StudentOfflineId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int RegistrationNumber { get; set; } } }
As we are going to update the Entity Models many times, in order to avoid the Run-Time Exception when the model changes and when we rerun the application, let us set the database initializer as DropCreateDatabaseIfModelChanges. So, modify the context class as follows. As you can see here, we have registered the two model classes within the context class using DbSet.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext : DbContext { public EFCodeFirstContext() : base("name=MyConnectionString") { //Setting the Database Initializer as DropCreateDatabaseIfModelChanges Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFCodeFirstContext>()); } public DbSet<StudentOnline> OnlineStudents { get; set; } public DbSet<StudentOffline> OfflineStudents { get; set; } } }
Please make sure to have the connection string with the name MyConnectionString within the app.config file or web.config file as shown in the below image.
Now, modify the Main method of the Program class as follows. Here, we are adding one StudentOnline entity to the database.
using System; namespace EFCodeFirstDemo { class Program { static void Main(string[] args) { using (EFCodeFirstContext context = new EFCodeFirstContext()) { var student = new StudentOnline() { FirstName = "Pranaya", LastName = "Rout" }; context.OnlineStudents.Add(student); context.SaveChanges(); Console.WriteLine("Student Added"); Console.ReadKey(); } } } }
Now, with the changes in place, if you run the application, then you will get the following exception and this makes sense. It is saying that you are trying to map the StudentOffline Entity which might be excluded from Entity Mapping by using the Ignore or NotMappedAttrubute data annotation. Please verify the same.
Now, you have two options. If you want that Entity to be created as a database table, then remove the NotMapped Attribute and if you do not want that Entity to be mapped to a database table, then remove that Entity from the context class. As we do not want that Entity to be mapped to a database, so, we need to remove the DbSet property which is created for StudentOffline Entity from the context class. So, modify the context class as follows.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext : DbContext { public EFCodeFirstContext() : base("name=MyConnectionString") { //Setting the Database Initializer as DropCreateDatabaseIfModelChanges Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFCodeFirstContext>()); } public DbSet<StudentOnline> OnlineStudents { get; set; } } }
With the above changes in place, now run the application and verify the database. Now, you can see only the StudentOnlines table and you can also verify that there is no column mapping for the RegistrationNumber property as shown in the below image.
The Entity Framework API will not create a column in the database table for a property if it does not have both getters or setters. For a better understanding, please modify the Student.cs class file as follows. As you can see, for the Property RegistrationNumber we have only get accessor, not the set accessor. And for the Property RollNumber, we have only set accessor, not the get accessor. So, in this case, for RollNumber and RegistrationNumber Properties, the corresponding database columns will not be created in the database.
namespace EFCodeFirstDemo { public class StudentOnline { private int _RollNumber; public int StudentOnlineId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int RegistrationNumber { get { return StudentOnlineId; } } public int RollNumber { set { _RollNumber = value; } } } }
Now, with the above changes in place, run the application and verify the database. Now, you can see, there is no column mapping for RegistrationNumber and RollNumber properties as shown in the below image.
In the next article, I am going to discuss the Required Attribute in Entity Framework Code First Approach with Examples. Here, in this article, I try to explain NotMapped Data Annotation Attribute in Entity Framework Code First Approach with Examples. I hope you enjoyed this NotMapped Attribute in Entity Framework Code First Approach with Examples article. Please give your valuable feedback and suggestions about this article.