How not persist property EF4 code first?

C#.NetEntity FrameworkEntity Framework-4Ef4 Code-Only

C# Problem Overview


How do I make non persisted properties using codefirst EF4?

MS says there is a StoreIgnore Attribute, but I cannot find it.

http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx

Is there a way to set this up using EntityConfiguration?

C# Solutions


Solution 1 - C#

In EF Code-First CTP5, you can use the [NotMapped] annotation.

using System.ComponentModel.DataAnnotations;
public class Song
{
    public int Id { get; set; }
    public string Title { get; set; }
    
    [NotMapped]
    public int Track { get; set; }

Solution 2 - C#

Currently, I know of two ways to do it.

  1. Add the 'dynamic' keyword to the property, which stops the mapper persisting it:

     private Gender gender;
     public dynamic Gender
     {
         get { return gender; }
         set { gender = value; }
     }
    
  2. Override OnModelCreating in DBContext and remap the whole type, omitting the properties you don't want to persist:

     protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
     {
         base.OnModelCreating(modelBuilder);
         modelBuilder.Entity<Person>().MapSingleType(p => new { p.FirstName, ... });
     }         
    

Using method 2, if the EF team introduce Ignore, you will be able to easily change the code to:

     modelBuilder.Entity<Person>().Property(p => p.IgnoreThis).Ignore();

Solution 3 - C#

If you don't want to use Annotations, you can use the Fluent API. Override the OnModelCreating and use DbModelBuilder's Ignore() method. Supposing you have a 'Song' entity:

public class MyContext : DbContext
{
	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		modelBuilder.Entity<Song>().Ignore(p => p.PropToIgnore);
	}
}

You can also use EntityTypeConfiguration to move configurations to separate classes for better manageability:

public class MyContext : DbContext
{
	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		modelBuilder.Configurations.Add(new SongConfiguration());
	}

}

public class SongConfiguration : EntityTypeConfiguration<Song>
{
	public SongConfiguration()
	{
		Ignore(p => p.PropToIgnore);
	}
}

Solution 4 - C#

I'm not sure if this is available yet.

On this MSDN page the Ignore Attribute and API are described but below, in the comments, somebody writes on 4 june 2010:

> You will be able to ignore properties in the next Code First release,

Solution 5 - C#

Add using System.ComponentModel.DataAnnotations.Schema to the model class. (Must include "SCHEMA")

Add [NotMapped] data annotation to the field(s) you want to keep from persisting (ie. not save to database).

This will prevent them from being added as a column to the table in the db.

Please note - previous answers may have included these bits, but they did not have the full "using" clause. They merely left off "schema" - under which the NotMapped attribute is defined.

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
QuestionAdam Gordon BellView Question on Stackoverflow
Solution 1 - C#Matt FrearView Answer on Stackoverflow
Solution 2 - C#Rob KentView Answer on Stackoverflow
Solution 3 - C#Marcos DimitrioView Answer on Stackoverflow
Solution 4 - C#Henk HoltermanView Answer on Stackoverflow
Solution 5 - C#wopaneseView Answer on Stackoverflow