62

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?

0

3 Answers 3

138

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

1
  • 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? Sep 16, 2014 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);
}
16

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

using System.ComponentModel.DataAnnotations.Schema;

to your class.

Not the answer you're looking for? Browse other questions tagged or ask your own question.