Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a property as NOT a column in EF 4.1

I have a class:

public class classParty
{
    private int _arrivedCount;

    public int PartyID {get; private set;}
    public DateTime PartyDate {get; private set;}
    public int ArrivedCount
    {
        get
        {
            return _arrivedCount;
        }

        set
        {
            _arrivedCount = value;
        }
    }
}

I can map the PartyId and the PartyDate but I don't have a column for ArrivedCount (it's a moment in time count, it doesn't persist).

How do I tell EF 4.1 to stop looking for a column named "ArrivedCount"? It's not in the table. It's not going to be in the table. It's simply a property of the object and that's all.

Thanks in advance.

EDIT: Here's the Fluent API configuration for classParty.

public class PartyConfiguration : EntityTypeConfiguration<classParty>
{
    public PartyConfiguration()
        : base()
    {
        HasKey(p => p.PartyID);

        Property(p => p.PartyID)
            .HasColumnName("PartyID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .IsRequired();
        Property(p => p.PartyDate)
            .HasColumnName("PartyDate")
            .IsRequired();

        ToTable("Party");
    }
}
like image 614
Tom Padilla Avatar asked Dec 09 '11 14:12

Tom Padilla


People also ask

What is NotMapped attribute?

The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database. In the following example, the AuditLog class will not be mapped to a table in the database: public class Contact.

Which method is used to apply configuration to entities or their properties in the entity Framework Core?

Property Mapping. The Property method is used to configure attributes for each property belonging to an entity or complex type.

What is mapping in EF?

It is a tool to access the database. More accurately, it's classified as an Object/Relational Mapper (ORM) which means it maps data in a relational database into objects of our applications.


1 Answers

With data annotations:

[NotMapped]
public int ArrivedCount
//...

Or using Fluent API:

modelBuilder.Entity<classParty>()
    .Ignore(c => c.ArrivedCount);
like image 189
Slauma Avatar answered Oct 20 '22 22:10

Slauma