64

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

139

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@DavidKirkland yes, it is quite possible. However, could lead to unwanted ignoring elsewhere. The Ignore() approach may be safer.
57

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);
}

1 Comment

NotMapped attribute works for generate FK right. If I use Fluent API with Ignore (builder.Ignore(c => c.Headers);) doesn't works. I have a List IEnumerable<MyTypeHeader> Headers
17

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

using System.ComponentModel.DataAnnotations.Schema;

to your class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.