Search code examples
c#entity-frameworkef-database-first

Add extra column to entity model which is not present in database


I have following Entity

[Table("classes")]
public class Class
{

    public int classID { get; set; }
    public Nullable<int> grade { get; set; }
    public string classname { get; set; }
    public Nullable<bool> Inactive { get; set; }
    public Nullable<int> total { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public Class()
    {}


}

This Entity is used at times by a stored procedure and at other times by entityframework (database first approach). Recently i added a field 'total' that is used by stored procedure. Now the entity framework gives error

'unknown column total in field list'

which is as expected.

My question is that is there a way i can set a default value in the onModelCreated function for total column so that it ignores the model change exception?

I am using EF6.


Solution

  • I think you can apply the [NotMapped] attribute to total to tell EF that it's not in the database