63

Entity Framework Code First will auto-create a table in the database base based on the Model.

Is there an attribute that will avoid this?

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Dozer
  • 5,025
  • 11
  • 36
  • 52

3 Answers3

138

Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Does this has the benefit (over fluent declaration) that annotated properties will (or should) be ignored by other data mappers/serializers, such as JSON serializers? – David Kirkland Sep 16 '14 at 12:37
55

Per the accepted answer and similar question/answer, in addition to [NotMapped] you can also specify it using the Fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<TheModelAffected>().Ignore(t => t.TheIgnoredProperty);
   base.OnModelCreating(modelBuilder);
}
Community
  • 1
  • 1
drzaus
  • 24,171
  • 16
  • 142
  • 201
17

[NotMapped] is the short version if you like conciseness. And of course, you would add:

using System.ComponentModel.DataAnnotations.Schema;

to your class.

SharpC
  • 6,974
  • 4
  • 45
  • 40
cyclical
  • 395
  • 4
  • 14