views:

146

answers:

2

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?

+2  A: 

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,

Henk Holterman
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
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
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
+1  A: 

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