How to ignore a property when using Entity Framework Code First

Entity FrameworkCode First

Entity Framework Problem Overview


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?

Entity Framework Solutions


Solution 1 - Entity Framework

Solution 2 - Entity Framework

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

Solution 3 - Entity Framework

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

using System.ComponentModel.DataAnnotations.Schema;

to your class.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDozerView Question on Stackoverflow
Solution 1 - Entity FrameworkSLaksView Answer on Stackoverflow
Solution 2 - Entity FrameworkdrzausView Answer on Stackoverflow
Solution 3 - Entity FrameworkcyclicalView Answer on Stackoverflow