51

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?

H H
  • 263,252
  • 30
  • 330
  • 514
Adam Gordon Bell
  • 3,083
  • 2
  • 26
  • 53
  • Any update on this? I'm using VS2010 with EF4.0 released and I still can't find StoreIgnoreAttribute. Did it get dropped? – Simon Gillbee Oct 25 '10 at 20:50

5 Answers5

66

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; }
Matt Frear
  • 52,283
  • 12
  • 78
  • 86
  • I get a compile time error if I try and use that attribute. Not defined. What reference and 'using' do we need for it? – ProfK Mar 12 '11 at 17:29
  • You might need to add a reference to System.ComponentModel.DataAnnotations.dll and using System.ComponentModel.DataAnnotations; I've updated my post with a code example. – Matt Frear Mar 25 '11 at 04:06
  • 1
    I encountered a bug when I first had a reference to System.ComponentModel.DataAnnotations.dll and then added the reference to EntityFramework.dll. The NotMapped attribute could not be resolved. Removing and re-adding the System.ComponentModel.DataAnnotations.dll reference resolved the issue. Happened in VS2010 SP1. – Eric J. Mar 19 '12 at 17:50
  • 7
    I used `[System.ComponentModel.DataAnnotations.Schema.NotMapped]` which seemed to have done the trick. – Clarice Bouwer Jan 15 '13 at 17:10
4

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();
Rob Kent
  • 5,183
  • 4
  • 33
  • 54
2

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);
    }
}
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
1

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,

H H
  • 263,252
  • 30
  • 330
  • 514
  • Wow, this seems like a huge whole in the code-first story. the code first entities will have a hard time making up a rich domain model, if they can't contain data as properties that is not directly a row in the database. – Adam Gordon Bell Aug 29 '10 at 16:16
  • 1
    The problem is that Code first is not yet RTM but only CTP so at the moment it is only for evalution not for production use. – Ladislav Mrnka Aug 29 '10 at 17:46
  • In the meantime, create additional 'properties' as methods rather than properties as these aren't persisted. Then you can continue to develop code against the appropriate objects. Once the StoreIgnore attribute is available, its an easy task to change back to a property and update all the references. – Clicktricity Sep 18 '10 at 21:03
0

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.