380

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?

6
  • 1
    See stackoverflow.com/questions/3595404/…
    – Sam Leach
    Commented Apr 30, 2012 at 14:23
  • It seems to me that it should apply to Entity Framework 4.1 as well. Check this link: social.msdn.microsoft.com/Forums/eu/adodotnetentityframework/…
    – ilmatte
    Commented Apr 30, 2012 at 14:40
  • 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? Commented Apr 30, 2012 at 14:42
  • 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. Commented Apr 30, 2012 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
    Commented Apr 30, 2012 at 14:52

2 Answers 2

737

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; }
}
7
  • 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. Commented Apr 30, 2012 at 21:58
  • 16
    With v5 > of EF these now live in System.ComponentModel.DataAnnotations.Schema
    – Daveo
    Commented Feb 3, 2013 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
    Commented Jul 5, 2013 at 7: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
    Commented Jul 28, 2016 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
    Commented Jan 2, 2017 at 15:30
41

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

1
  • 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
    Commented Jan 13, 2016 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.