Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Entity Framework 4.1 Codefirst, how do you create unmapped fields in the poco classes?

I have a set of classes as my domain objects. I have a set of configuration files to map these objects (EntityTypeConfiguration<>).

When I add a property to any of the domain objects without mapping to a column, the dbcontext attempts to query for the column, ignoring the fact that it is not mapped.

I must be missing a setting either in the configuration code or the dbcontext code. I do not want to add an attribute to the poco class (decorating the pocos tie them to a specific persistence implementation, which I wish to avoid).

On the call against the IQueryable to populate a ticket object, the call fails with the message:

Invalid column name 'NotInDatabase'.

public class Ticket  
{  
    public Ticket() 
    {
    }

    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Title
    {
        get;
        set;
    }

    public virtual string Description
    {
        get;
        set;
    }

    public string NotInDatabase
    {
        get;
        set;
    }
}


internal class TicketConfiguration : EntityTypeConfiguration<Ticket>  
{   
    public TicketConfiguration()  
    {  
        ToTable("ticket_table_name");

        HasKey(o => o.Id)
            .Property(o => o.Id)
            .HasColumnName("ticketId")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .IsRequired();

        Property(o => o.Title).HasColumnName("TicketTitle");
        Property(o => o.Description).HasColumnName("TicketDescription");
    }   
}

Note: Please do not suggest using "Database First" or "Model First" for my situation. I want to map poco objects to the database using the features of code first, even though I have an existing db structure. I am comparing this to nhibernate and really want to stick to a similar structure (since Microsoft "adopted" fluent nhibernate's approach, it's pretty easy to compare apples to apples).

Thanks!

like image 555
junkyspace Avatar asked May 10 '11 17:05

junkyspace


1 Answers

.Ignore should do the trick or by attribute it's called [NotMapped]

like image 126
jeriley Avatar answered Feb 15 '23 21:02

jeriley