Donate

Exclude Property Mapping In Entity Framework Code First Approach

Hi all,
Given a database table such as User with fields Id, UserName and Password that will be mapped to a class using Entity Framework called User with an additional field of ConfirmPassword.
public class User 
{
 public int Id { get; set; }

 public string UserName { get; set; }

 public string Password { get; set; }
 
 public string ConfirmPassword { get; set; }
}
I would like to exclude the ConfirmPassword field from being mapped to the User table. So, after doing some searching, the solution is to decorate the property with [NotMapped] attribute and make sure to reference the DataAnnotations namespace (System.ComponentModel.DataAnnotations.Schema).
using System.ComponentModel.DataAnnotations.Schema;
public class User : IEntity
{
 public int Id { get; set; }

 public string UserName { get; set; }

 public string Password { get; set; }

 [NotMapped] 
 public string ConfirmPassword { get; set; }
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input