ChatGPT解决这个技术问题 Extra ChatGPT

Ignoring a class property in Entity Framework 4.1 Code First

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?

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/…
@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?
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.
I'm sorry but I did a test right now with a simple application and it works. Could you provide sample code?

C
Community

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; }
}

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.
With v5 > of EF these now live in System.ComponentModel.DataAnnotations.Schema
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?
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.
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
S
Shane Kenyon

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


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.