Monday, November 28, 2016

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; }
}

0 comments:

Post a Comment