Peter
Peter

Reputation: 11890

Entity Framework - How to prevent a model property from column creation?

Folks,

I am developing an web application based on ASP .NET MVC 4.

Let's say I define a model such as:

public class MyUser {
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

The database initializer code will automatically create a table with three columns - UserName, FirstName, and LastName.

However, let's say I don't want LastName to be part of the database table.

Is there any data annotation attribute that I can use to prevent a property from being exposed as a column?

Thank you in advance for your help.

Regards,
Peter

Upvotes: 6

Views: 3871

Answers (1)

J.W.
J.W.

Reputation: 18181

use NotMapped attribute. Here is a very good reference about different attribute you can use.

public class MyUser {
    public string UserName { get; set; }
    public string FirstName { get; set; }
    [NotMapped]
    public string LastName { get; set; }

}

Upvotes: 11

Related Questions