369

My understanding is that the [NotMapped] attribute is not available until EF 5 which is currently in CTP so we cannot use it in production.

How can I mark properties in EF 4.1 to be ignored?

UPDATE: I noticed something else strange. I got the [NotMapped] attribute to work but for some reason, EF 4.1 still creates a column named Disposed in the database even though the public bool Disposed { get; private set; } is marked with [NotMapped]. The class implements IDisposeable of course but I don't see how that should matter. Any thoughts?

SharpC
  • 6,974
  • 4
  • 45
  • 40
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • 1
    See http://stackoverflow.com/questions/3595404/how-not-persist-property-ef4-code-first – Sam Leach Apr 30 '12 at 14:23
  • 1
    @SamLeach: I already read that question before posting my own and did not find a solution. The question is how to achieve the same in EF4? Have I missed something in that post? – Raheel Khan Apr 30 '12 at 14:42
  • It seems to me that it should apply to Entity Framework 4.1 as well. Check this link: http://social.msdn.microsoft.com/Forums/eu/adodotnetentityframework/thread/69e99bf3-44cb-4b6b-9272-f5c3a909dcce – ilmatte Apr 30 '12 at 14:40
  • That's what I thought as well since I have EF 4.1 and can see the attribute. But EF seems quite happy to completely ignore it in my applications. This is true of native and reference types as well as com reference types such as MS office intrerop applications. – Raheel Khan Apr 30 '12 at 14:45
  • I'm sorry but I did a test right now with a simple application and it works. Could you provide sample code? – ilmatte Apr 30 '12 at 14:52
  • Crap. That tells me I'm doing something else wrong. Difficult to provide code since it's a 300 entity long hierarchy. But you have answered the question. Thanks. – Raheel Khan Apr 30 '12 at 14:54

2 Answers2

721

You can use the NotMapped attribute data annotation to instruct Code-First to exclude a particular property

public class Customer
{
    public int CustomerID { set; get; }
    public string FirstName { set; get; } 
    public string LastName{ set; get; } 
    [NotMapped]
    public int Age { set; get; }
}

[NotMapped] attribute is included in the System.ComponentModel.DataAnnotations namespace.

You can alternatively do this with Fluent API overriding OnModelCreating function in your DBContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Customer>().Ignore(t => t.LastName);
   base.OnModelCreating(modelBuilder);
}

http://msdn.microsoft.com/en-us/library/hh295847(v=vs.103).aspx

The version I checked is EF 4.3, which is the latest stable version available when you use NuGet.


Edit : SEP 2017

Asp.NET Core(2.0)

Data annotation

If you are using asp.net core (2.0 at the time of this writing), The [NotMapped] attribute can be used on the property level.

public class Customer
{
    public int Id { set; get; }
    public string FirstName { set; get; } 
    public string LastName { set; get; } 
    [NotMapped]
    public int FullName { set; get; }
}

Fluent API

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().Ignore(t => t.FullName);
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<Customer> Customers { get; set; }
}
Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 3
    Thanks. It is always good to see answers with effort behind them. I actually did not notice your last line before marking the another answer as correct. Since your answer did come in before, I think it is only fair to change yours to correct. I am sure @ilmatte won't mind. – Raheel Khan Apr 30 '12 at 21:58
  • 16
    With v5 > of EF these now live in System.ComponentModel.DataAnnotations.Schema – Daveo Feb 03 '13 at 11:48
  • when I use FluentMApping, the field is still returned in the resultset, albeit it NULL know. How do I get it to not even be returned? – Rodney Jul 05 '13 at 07:03
  • 9
    Using [NotMapped] will also tell other serializers (JSON / XML for example) to not map this property. If you wish to only prevent mapping to a database table, but still have serializers access the property, you have to use the OnModelCreating approach. – maxmantz Jul 28 '16 at 11:20
  • Although I havent exactly figured out how, This does not work with properties inherited on entity framework 6+ the attribute and fluent api seem to ignore the instruction – PBo Jan 02 '17 at 15:30
  • Hello, Is it possible to bypass the property only in get? For example. I do not want the user password to be loaded on the read. – Gus Nov 17 '17 at 16:58
40

As of EF 5.0, you need to include the System.ComponentModel.DataAnnotations.Schema namespace.

Shanerk
  • 5,175
  • 2
  • 40
  • 36
  • 2
    Is this still the case? I'm using EF 6.0 and this is no longer the case at least not for my project. Unless I've referenced a namespace which already somewhere references the above. – JARRRRG Jan 13 '16 at 16:26