Back to: ASP.NET Core Tutorials For Beginners and Professionals
NotMapped Attribute in Entity Framework Core (EF Core)
In this article, I am going to discuss NotMapped Data Annotation Attribute in Entity Framework Core (EF Core) with Examples. Please read our previous article, where we discussed InverseProperty Attribute in Entity Framework Core with Examples.
NotMapped Attribute in Entity Framework Core:
The NotMapped Attribute in Entity Framework Core 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, EF Core creates a column for each property (property having the get and set accessors) in an entity class.
Using 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 EF Core follows.
Examples to Understand NotMapped Attribute in EF Core:
Before using the NotMapped Attribute in Entity Framework Core, let us first see the signature of this Attribute class. If you go to the definition of NotMappedAttribute class, then you will see the following. The NotMapped Attribute denotes that 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 create a class file with the name the StudentOnline.cs class and then copy and paste the following code into it. As you can see, we created the class with a set of properties here. Then we applied NotMapped Attribute with RegistrationNumber Property, which should exclude the corresponding database column while creating the StudentOnline database table.
using System.ComponentModel.DataAnnotations.Schema; namespace EFCoreCodeFirstDemo.Entities { 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; } } }
Please create another class file with the name the StudentOffline.cs class and then copy and paste the following code into it. As you can see, we also created the class with the same set of properties here. Then we applied the NotMapped attribute on the StudentOffline Entity class, which should exclude the corresponding database table to be created in the database.
using System.ComponentModel.DataAnnotations.Schema; namespace EFCoreCodeFirstDemo.Entities { //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; } } }
Next, modify the context class as follows. As you can see here, we have registered the two model classes within the context class using DbSet properties.
using Microsoft.EntityFrameworkCore; namespace EFCoreCodeFirstDemo.Entities { public class EFCoreDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=LAPTOP-6P5NK25R\SQLSERVER2022DEV;Database=EFCoreDB;Trusted_Connection=True;TrustServerCertificate=True;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { } public DbSet<StudentOnline> OnlineStudents { get; set; } public DbSet<StudentOffline> OfflineStudents { get; set; } } }
Note: Before proceeding further, I am deleting the Migration Folder from our project and the database.
With the changes in place, open Package Manager Console and Execute the following add-migration and update-database commands. You can give any name to your migration. Here, I am giving DBMigration1. The name that you are giving it should not be given earlier.
Now, if you verify the database, you will only see the StudentOnlines table, and you can also verify that there is no column mapping for the RegistrationNumber property, as shown in the image below.
Note: Even though we have specified the StudentOffline entity in the context class, the corresponding database table is not generated. This is because we have applied the StudentOffline entity with the NotMapped Attribute.
The Entity Framework Core 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 StudentOnline.cs class file as follows. As you can see, for the Property RegistrationNumber, we 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 EFCoreCodeFirstDemo.Entities { public class StudentOnline { private int _RollNumber; //Properties Having both get and set Accessor public int StudentOnlineId { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } //Property Having only get Accessor public int RegistrationNumber { get { return StudentOnlineId; } } //Property Having only set Accessor public int RollNumber { set { _RollNumber = value; } } } }
With the changes in place, open Package Manager Console and Execute the following add-migration and update-database commands. You can give any name to your migration. Here, I am giving DBMigration2. The name that you are giving it should not be given earlier.
If you verify the database, you will see no column mapping for RegistrationNumber and RollNumber properties, as shown in the image below.
In the next article, I am going to discuss the Required Attribute in Entity Framework Core with Examples. In this article, I try to explain NotMapped Data Annotation Attribute in Entity Framework Core with Examples. I hope you enjoyed this NotMapped Attribute in EF Core with Examples article.