6
I’d like to know what the [NotMapped]
and when to use it?
6
I’d like to know what the [NotMapped]
and when to use it?
8
When we are creating an application using the ORM Entity Framework, we can use the Code First in which we model the application and from which our database will be generated.
To model the tables in our database, we can create classes in the C# using the Dataannotations, thus defining primary key, maximum size, constraints, etc.
The attribute NotMapped
has the function of saying that the property that has this attribute will not be mapped to the table, that is, it will not contain this field in our table. Then why do we need it? Simple, sometimes we need to persist some important information for our application, but it does not need to be saved in the database.
An example, let’s assume that we need a person’s age, we already have the date of birth persisted in the bank, so we don’t need to save age (it would just be the most information saved unnecessarily), so we can do this logic in memory:
public class Pessoa
{
[Key]
public int Id { get; set; }
[MaxLength(80)]
public string Nome { get; set; }
public DateTime DataNascimento { get; set; }
[NotMapped]
public int Idade
{
get
{
if (DateTime.Now.Month >= DataNascimento.Month && DateTime.Now.Day >= DataNascimento.Day)
return DateTime.Now.Year - DataNascimento.Year;
return DateTime.Now.Year - DataNascimento.Year - 1;
}
}
[NotMapped]
public bool MaioridadeCivil
{
get
{
return Idade >= 18;
}
}
}
Now whenever we have an entity Pessoa
in our hands we possess the age and the information if it possesses civil majority in memory and we do not have to persist one more information in the database.
For more information, curiosities or questions about the content of Entity Framework, check this link: http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx
7
This is used in the Code First methodology of the Entity Framework where you model a class with the properties it should have and it is transformed into a table with the columns described in that class and the database is created. Some of these properties may be interesting to have only in memory, it may be part of the mechanism you have in the application, but it is not part of the model, it should not be persisting in the table, it may be just something auxiliary, even something calculated. You need something that expresses that the property should not be mapped to a column in the database table and this is solved with the attribute NotMapped
.
Let’s say you have a date of birth property and it persists. You want to have a enabling property that gives you age, but it doesn’t need to be persisted, you get its value by calculating the date of birth with the current date, you just want it in the class. Obviously it will not have a value, only a calculation, but nothing prevents it from having a value, just understand that it will not be persisted.
Even a class can be annotated like this, so the whole class will not turn into a table.
I answered a question where this attribute is used and a code was written in the application to handle it:
using System;
using static System.Console;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public class Program {
public static void Main() {
foreach (var c in GetColumns()) WriteLine(c);
}
private static IEnumerable<string> GetColumns() => typeof(Usuario).GetProperties()
.Where(e => e.Name != "Id" && !(Attribute.GetCustomAttribute(e, typeof(NotMappedAttribute)) is NotMappedAttribute))
.Select(e => e.Name);
}
public class Usuario
{
[Required]
public string Nome { get; set; }
[Required]
public string Login { get; set; }
[Required]
public string Password { get; set; }
[NotMapped]
[Required]
public string ConfirmPassword { get; set; }
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Browser other questions tagged c#
You are not signed in. Login or sign up in order to post.