60

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
  • 18,664
  • 18
  • 94
  • 132
Dozer
  • 4,697
  • 11
  • 34
  • 52

3 Answers3

131

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

Kirk Woll
  • 73,473
  • 21
  • 178
  • 189
SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
  • 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
53

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
  • 23,063
  • 16
  • 132
  • 194
16

[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,220
  • 4
  • 42
  • 39
cyclical
  • 385
  • 5
  • 14