Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3: Attribute for not mapping a property to a DB column

I’m using ASP.NET MVC3. I have a model that has one property that I don’t want to store in the database. Is there an attribute that I can put on the property to achieve this? Thanks.

like image 398
Dandan Avatar asked Aug 30 '11 19:08

Dandan


2 Answers

public class Person
{
    [Key]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

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

The attribute are in the namespace System.ComponentModel.DataAnnotations

like image 81
Major Byte Avatar answered Oct 21 '22 06:10

Major Byte


Just to add more options... this is why I prefer to keep my domain model separate from my view model. My view model often has additional fields necessary for rendering the view which does not belong in the domain model. The design I typically use is described pretty well here.

like image 21
Timothy Strimple Avatar answered Oct 21 '22 05:10

Timothy Strimple